Practical Approach of Getting Directory List and Its Details From FTP Path


In this article we are going to see how to fetch directory and file details from the FTP path.

Step 1: Used Namespaces:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

Step 2: Usage:

GetListDirectoryDetails ("ftp://208.43.121.51/", "Username", "Password");

Step 3: Creating FTP request by specifying file name and path:

public static FtpWebRequest CreateFTPRequest(string fullFtpPath)
{
    return (FtpWebRequest)FtpWebRequest.Create(new Uri(fullFtpPath));
}

Step 4: Setting User Name and Password for FTP path:

public static NetworkCredential CreateNetworkCredentials(string ftpUserID, string
ftpPassword)
{
 return new NetworkCredential(ftpUserID, ftpPassword);
}

Step 5: Getting current response if any error occurs:

public static HttpResponse GetHttpResponse()
{
    return HttpContext.Current.Response;
}

Step 6: Setting of FTP request:

public static FtpWebRequest GetFTPWebRequest(string fullFtpPath,
            string ftpUserID, string ftpPassword)
{
    FtpWebRequest reqFTP = CreateFTPRequest(fullFtpPath);
reqFTP.Credentials = CreateNetworkCredentials(ftpUserID, ftpPassword);
        reqFTP.Method = WebRequestMethods.Ftp. ListDirectoryDetails;

        reqFTP.UseBinary = true; // Specify the data transfer type.
        return reqFTP;
    }

Step 7: Used to get the list directory details as an Array:

public static string[] GetListDirectoryDetails(string fullFtpPath, string ftpUserID, string ftpPassword)
{
    //Create a Ftp Web request.
    FtpWebRequest fwr = GetFTPWebRequest(fullFtpPath, ftpUserID, ftpPassword);

    //Getting Response from the Request.
    WebResponse response = fwr.GetResponse();

    //Used to get all the File as list with details.
    return GetFilesDetailList(response);
}

Step 8: Used to get the entire File as a list with details:

public static string[] GetFilesDetailList(WebResponse response)
{
    string[] files;

    try
    {
//Reading the response.
StreamReader reader = new StreamReader(response.GetResponseStream());
        string line = reader.ReadLine();
        StringBuilder result = new StringBuilder();

        while (line != null)
        {
            result.Append(line);
            result.Append("\n");
            line = reader.ReadLine();
        }

        result.Remove(result.ToString().LastIndexOf("\n"), 1);
        reader.Close();
        response.Close();
        files = result.ToString().Split('\n');
    }
    catch (Exception ex)
    {
        files = null;
        GetHttpResponse().Write(ex.Message + " Retrieving File Error");
    }

    return files;
}

Step 9: COPY & PASTE Code Snippet

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace ApplicationClassLibrary
{
    class FTPDirectoryList
    {
        //Used to get list directory details.
        public static string[] GetListDirectoryDetails(string fullFtpPath, string ftpUserID, string ftpPassword)
        {
            //Create a Ftp Web request.
            FtpWebRequest fwr = GetFTPWebRequest(fullFtpPath, ftpUserID, ftpPassword);

            //Getting Response from the Request.
            WebResponse response = fwr.GetResponse();

            //Used to get all the File as list with details.
            return GetFilesDetailList(response);
        }
 
        //Setting of FTP request
        public static FtpWebRequest GetFTPWebRequest(string fullFtpPath,
            string ftpUserID, string ftpPassword)
        {
            FtpWebRequest reqFTP = CreateFTPRequest(fullFtpPath);
            reqFTP.Credentials = CreateNetworkCredentials(ftpUserID, ftpPassword);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

            reqFTP.UseBinary = true; // Specify the data transfer type.
            return reqFTP;
        }

        //Creating FTP request by specifying file name and path
        public static FtpWebRequest CreateFTPRequest(string fullFtpPath)
        {
            return (FtpWebRequest)FtpWebRequest.Create(new Uri(fullFtpPath));
        }

        //Setting UserName and Password for FTP path.
        public static NetworkCredential CreateNetworkCredentials(string ftpUserID, string ftpPassword)
        {
            return new NetworkCredential(ftpUserID, ftpPassword);
        }

        //Used to get all the File as list with details.
        public static string[] GetFilesDetailList(WebResponse response)
        {
            string[] files;
 
            try
            {
                //Reading the response.
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                StringBuilder result = new StringBuilder();
 
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }

                result.Remove(result.ToString().LastIndexOf("\n"), 1);
                reader.Close();
                response.Close();
                files = result.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                files = null;
                GetHttpResponse().Write(ex.Message + " Retrieving File Error");
            }

            return files;
        }

        //Gettting Current Response to If any error occurs.
        public static HttpResponse GetHttpResponse()
        {
            return HttpContext.Current.Response;
        }
    }
}


Thanks for reading this article. Have a nice day.


Similar Articles