将文件从ftp下载到本地驱动器

将文件从ftp下载到本地驱动器,ftp,Ftp,在FTP子文件夹中包含一些csv文件,这些文件要下载到本地驱动器文件夹中的一个csv文件中。 在FTP中,每个csv文件只包含一条记录。所以,现在我想将所有5条记录放在一个csv文件的localdrive文件夹中。下面的代码仅适用于一个csv文件 private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath) {

在FTP子文件夹中包含一些csv文件,这些文件要下载到本地驱动器文件夹中的一个csv文件中。 在FTP中,每个csv文件只包含一条记录。所以,现在我想将所有5条记录放在一个csv文件的localdrive文件夹中。下面的代码仅适用于一个csv文件

   private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
   {
       //FileStream responseStream =null;
        int Length = 2048;
        Byte[] buffer = new Byte[Length];


     //   int bytesRead = responseStream.Read(buffer, 0, Length);
        int bytesRead = 0;
        FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, false);

        request.Method = WebRequestMethods.Ftp.DownloadFile;

        Stream reader = request.GetResponse().GetResponseStream();
        FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);

        while (true )
        {
            bytesRead = reader.Read(buffer,0,buffer.Length);
            if (bytesRead == 0)
                break;


            fileStream.Write(buffer, 0, bytesRead);
        }

        fileStream.Close();
    }
    private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));

        //Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
        request.Proxy = null;

        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = keepAlive;

        request.Credentials = new NetworkCredential(userName, password);

        return request;
    }


    private void button1_Click(object sender, EventArgs e)
    {
        DownloadFile("Username1", "Password1", "ftp://172.32.1.252:5010/Test/CBRE/building.csv", "C://Workspace/ex.csv");

    }

我不知道你到底想做什么,但你也可以试试这个:

WebClient Client = new WebClient ();
Client.DownloadFile(http://www.domain.com/files/yourfile.ext, " yourfile.ext");