1

I need to write a routines that downloads a given directory on the server completely - all the files and directories in it.

Right now I have a routine that lists dir content

public List<string> GetListOfFiles(string serverPath)
        {
            List<string> files = new List<string>();
            try
            {

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + serverPath);               
                request.Credentials = new NetworkCredential(_domain + "\\" + _username, _password);
                request.Method = WebRequestMethods.Ftp.ListDirectory;               


                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());

                string line = reader.ReadLine();
                while (line != null)
                {
                    files.Add(line);                    
                    line = reader.ReadLine();
                }
                response.Close();

            }           
            catch (WebException ex)
            {
                FtpWebResponse response = (FtpWebResponse)ex.Response;
                string exMsg = string.Empty;

                switch (response.StatusCode)
                {
                    case FtpStatusCode.NotLoggedIn:
                        exMsg = "wrong username/password";
                        break;   


                    default:
                        exMsg = "The server is inaccessible or taking too long to respond.";
                        break;
                }   


                throw new Exception(exMsg);
            }
            return files;

        }

The issue is that I get the list of files and directories...so something like

file1.dll
file2.dll
dir1Name

Is there a way to make a distinction between a file name and a directory name when listing it? Like a flag?

1
  • 3
    The response returned by ListDirectory is not standardized and can vary based on the software the server is running. Because of this, there isn't a good way to tell directories from files. Commented Sep 25, 2012 at 20:29

1 Answer 1

1

Unfortunately, the returned information is really a function of your FTP server, not the framework.

You can ListDirectoryDetails instead of ListDirectory, which should provide you much more detailed information (including whether each file is a directory or a file), but would require special parsing, as its format, too, is dependent on the FTP Server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.