Click here to Skip to main content
15,889,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm trying to get the files from a remote FTP folder. I'm using the method WebRequestMethods.Ftp.ListDirectory for getting the files. But this method is giving only the directories in that folder. Is there any to get only the sub folders of a remote path? I want to take all the files inside the remote folder, including the files in the sub folders.

I'm using the following code now

C#
StringBuilder result = new StringBuilder();
           WebResponse response = null;
           StreamReader reader = null;
           try
           {
               FtpWebRequest reqFTP;
               reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strServer + "/" + strRemotePath + "/"));
               reqFTP.UseBinary = true;
               reqFTP.Credentials = new NetworkCredential(strUsername, strPassword);
               reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
               reqFTP.Proxy = null;
               reqFTP.KeepAlive = false;
               reqFTP.UsePassive = false;
               response = reqFTP.GetResponse();
               reader = new StreamReader(response.GetResponseStream());
               string line = reader.ReadLine();
               while (line != null)
               {
                   DataRow drNewFile = dsFTPItems.Tables["file"].NewRow();
                   drNewFile["name"] = line;
                   drNewFile["folder-path_Id"] = dsFTPItems.Tables["folder-path"].Rows.Count - 1;
                   dsFTPItems.Tables["file"].Rows.Add(drNewFile);
                   line = reader.ReadLine();
               }
           }
           catch
           {
               if (reader != null)
               {
                   reader.Close();
               }
               if (response != null)
               {
                   response.Close();
               }
           }

With regards
Nidhin
Posted
Updated 24-Aug-11 18:32pm
v2
Comments
walterhevedeich 25-Aug-11 0:34am    
Im quite confused by your question. Do you want just the subfolders, or do you want subfolders and files?

1 solution

Hi, Nidhin;

Your formatting of the FTP Server URI looks correct, as does the setup of the FtpWebRequest to connect to the server.

Instead of polling the FTP Server using WebRequestMethods.Ftp.ListDirectory, have you tried using WebRequestMethods.Ftp.ListDirectoryDetails? Your FTP Request method would look like the following:

C#
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;


The WebRequestMethods.Ftp.ListDirectory would send the FTP NLIST command to the server, where as WebRequestMethods.Ftp.ListDirectoryDetails would send the FTP LIST command to the server. The FTP LIST command will return both the folders and files in the specified remote directory URI, and in case the remote directory on the FTP server is empty, it will also return the file nodes "." and "..". You will have to parse the results stream to get the remote directory contents, and your parsing should be able to differentiate between files and folders or directories.

With regards to how you're parsing the results, I can follow your higher level intent for looking at the text response from the FTP Server using the while() loop, but I can't quite see how the DataRow and corresponding DataSet objects are initialized and used to parse the remote directory contents actually returned.

If it's of any help, the following code fragment will initialize a StreamReder instance named remoteDirContents, which will then be used to format and "build up" a StringBuilder instance named remoteFiles, that will contain the full contents from executing the WebRequestMethods.Ftp.ListDirectoryDetails request. Each entry in the remote FTP directory parsed out will be in the string variable directoryData:

C#
StreamReader remoteDirContents =
new StreamReader(response.GetResponseStream());

if (remoteDirContents == null)
{
    //
    // Add appropriate error handling here, and exit out
    // of your function as needed if we can't read the FTP
    // Request's response stream...
    //
}

bool finished = false;
string directoryData = string.Empty;
StringBuilder remoteFiles = new StringBuilder();

do
{
    directoryData = remoteDirContents.ReadLine();

    if (directoryData != null)
    {
        if (remoteFiles.Length > 0)
        {
            remoteFiles.Append("\n");
        }

        remoteFiles.AppendFormat("{0}", directoryData);
    }

    else
    {
        finished = true;
    }
}
while (!finished);


I hope this was of help and interest.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900