How to List Directory Contents with FTP in C#?

后端 未结 7 706
时光取名叫无心
时光取名叫无心 2020-11-27 18:08

How to List Directory Contents with FTP in C# ?

I am using below code to List Directory Contents with FTP it is returning result in XML format ,but i want only the

相关标签:
7条回答
  • 2020-11-27 18:16

    If you want to list the name of the files that are inside de directory, you have to put (reqFTP.Proxy = null;) before you invoke (reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;).

    Hope this can help you!

    0 讨论(0)
  • 2020-11-27 18:25

    Some proxies reformat the directory listing, so it's quite difficult to parse a directory listing reliably unless you can guarantee that the proxy doesn't change

    0 讨论(0)
  • 2020-11-27 18:25

    Simplest and most Efficient way to Get FTP Directory Contents:

    var contents = GetFtpDirectoryContents(new Uri("ftpDirectoryUri"), new NetworkCredential("userName", "password"));

        public static List<string> GetFtpDirectoryContents(Uri requestUri, NetworkCredential networkCredential)
        {
            var directoryContents = new List<string>(); //Create empty list to fill it later.
            //Create ftpWebRequest object with given options to get the Directory Contents. 
            var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.ListDirectory);
            try
            {
                using (var ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse()) //Excute the ftpWebRequest and Get It's Response.
                using (var streamReader = new StreamReader(ftpWebResponse.GetResponseStream())) //Get list of the Directory Contentss as Stream.
                {
                    var line = string.Empty; //Initial default value for line.
                    do
                    {
                        line = streamReader.ReadLine(); //Read current line of Stream.
                        directoryContents.Add(line); //Add current line to Directory Contentss List.
                    } while (!string.IsNullOrEmpty(line)); //Keep reading while the line has value.
                }
            }
            catch (Exception) { } //Do nothing incase of Exception occurred.
            return directoryContents; //Return all list of Directory Contentss: Files/Sub Directories.
        }
    
        public static FtpWebRequest GetFtpWebRequest(Uri requestUri, NetworkCredential networkCredential, string method = null)
        {
            var ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri); //Create FtpWebRequest with given Request Uri.
            ftpWebRequest.Credentials = networkCredential; //Set the Credentials of current FtpWebRequest.
    
            if (!string.IsNullOrEmpty(method))
                ftpWebRequest.Method = method; //Set the Method of FtpWebRequest incase it has a value.
            return ftpWebRequest; //Return the configured FtpWebRequest.
        }
    
    0 讨论(0)
  • 2020-11-27 18:28

    Try this:

    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
    ftpRequest.Credentials =new NetworkCredential("anonymous","janeDoe@contoso.com");
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
    FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
    StreamReader streamReader = new StreamReader(response.GetResponseStream());
    
    List<string> directories = new List<string>();
    
    string line = streamReader.ReadLine();
    while (!string.IsNullOrEmpty(line))
    {
        directories.Add(line);
        line = streamReader.ReadLine();
    }
    
    streamReader.Close();
    

    It gave me a list of directories... all listed in the directories string list... tell me if that is what you needed

    0 讨论(0)
  • 2020-11-27 18:29

    You are probably looking for PrintWorkingDirectory

    0 讨论(0)
  • 2020-11-27 18:32

    You need ListDirectory that lists the directory contents

    EDIT: Or you can use this Chilkat library that wraps it up nicely for you

    0 讨论(0)
提交回复
热议问题