简体   繁体   English

如何列出ftp服务器上目录中的文件?

[英]How can I list the files in a directory on ftp server?

The problem is the following: 问题如下:

I found a solution which can list the files and directories in the root of ftp server. 我找到了一种解决方案,可以列出ftp服务器根目录中的文件和目录。 But I need the content of the directories also. 但是我也需要目录的内容。 How need I modify this method? 我需要如何修改此方法?

internal void ListFilesOnServer()
        {
            try
            {
                FtpWebRequest ftpwrq = (FtpWebRequest)WebRequest.Create(server);
                ftpwrq.Credentials = new NetworkCredential(user,passw);
                ftpwrq.Method = WebRequestMethods.Ftp.ListDirectory;
                ftpwrq.KeepAlive = false;
                FtpWebResponse fresponse = (FtpWebResponse)ftpwrq.GetResponse();
                StreamReader sr = new StreamReader(fresponse.GetResponseStream());
                StreamWriter sw = new StreamWriter(new FileStream("test.txt", FileMode.Create));
                sw.WriteLine(sr.ReadToEnd());
                sw.Close();
                fresponse.Close();
                sr.Close();
                MessageBox.Show("Gotcha");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

You can do it using the following code: 您可以使用以下代码进行操作:

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(sUri));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(sFtpUserID, sFtpPassword);
            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)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM