【问题标题】:Get files list from Folder through FTP using c# [duplicate]使用c#通过FTP从文件夹中获取文件列表[重复]
【发布时间】:2017-11-04 12:43:05
【问题描述】:

我想使用 c# 通过 FTP 从文件夹中获取文件,我有一个名为 MyFolder 的文件夹名称,在这个文件夹中我有多个文件夹,我需要从所有这些文件夹中获取每个文件在我的 MyFolder 中。下面的代码是我获取所有目录的代码,现在我需要获取每个文件。

 Eg:httpdocs/Myfolder/newfolder/newfile.txt 
                               /newfile1.txt  
                               /newfile2.txt 
    httpdocs/Myfolder/newfolder1/newfile.txt 
    httpdocs/Myfolder/newfolder2/newfile.txt   


        FtpWebRequest ftpRequest =(FtpWebRequest)WebRequest.Create("ftp://www.xxxxxxx.com/httpdocs/MyFolder");
        ftpRequest.Credentials = new NetworkCredential("xxxxx", "xxxxxx");
        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();
    }

【问题讨论】:

    标签: c# ftp


    【解决方案1】:

    您看过 MSDN 文档吗? https://msdn.microsoft.com/de-de/library/ms229711(v=vs.110).aspx

    public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.DownloadFile;
    
            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());
    
            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
    
            reader.Close();
            response.Close();  
        }
    

    编辑:Already on StackOverflow 看看这里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-30
      • 2013-09-26
      • 1970-01-01
      • 2011-04-29
      • 2013-12-29
      • 2015-10-03
      相关资源
      最近更新 更多