使用C#在FTP中并行进行多个下载/上传

我想在不使用FTPWebRequest的情况下使用C#在FTP中并行进行多个下载/上传。 我编写了我的自定义代码,当我尝试同时下载两个文件时,第一个正确下载,而第二个显示大小为0 KB(它也下载)。

public void sendCommand(String command, params string[] strfilename) { if (command == "RETR ") //Downloading file from Server { FileStream output = null; if (!File.Exists(strfilename[0])) output = File.Create(strfilename[0]); else output = new FileStream(strfilename[0] , FileMode.Open); command = "RETR " + strfilename[0]; Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray()); clientSocket.Send(cmdBytes, cmdBytes.Length, 0); Socket csocket = createDataSocket(); DateTime timeout = DateTime.Now.AddSeconds(this.timeoutSeconds); while (timeout > DateTime.Now) { this.bytes = csocket.Receive(buffer, buffer.Length, 0); output.Write(this.buffer, 0, this.bytes); if (this.bytes <= 0) { break; } } // this.BinaryMode = true; output.Close(); if (csocket.Connected) csocket.Close(); this.readResponse(); MessageBox.Show("File Downloaded successfully"); else if....so on } } 

在我的主要方法中,我喜欢这样:

 ftpcommand.sendCommand("RETR ","RMSViewer.xml"); //Downloading from Server ftpcommand.sendCommand("RETR ","cms.xml");//Downloading from Server 

任何代码片段….

正如Dave所说,你需要单独的ftpCommand类实例。 查看使用BackgroundWorker在后台运行命令(异步)。

你是如何同时发出请求的?

螺纹?

如果是这样 – 您可能希望确保创建“ftpcommand”类的单独实例。

我想我们需要更多的信息来帮助你:)