0
我想我应该从一个函数,应该从ftp服务器下载文件的内存问题。我认为这是一个记忆事物的原因是因为它在调试期间工作正常(也许它给垃圾收集器更多的时间)。然而我认为使用应该解决这个问题......使用FTP下载多个文件只在运行时崩溃(不调试)
只是为了清楚该函数调用一次,但在for循环中调用多次调用错误消息:550指定的网络名称不再可用。
请帮 阿萨夫
private void downloadFile(string sourceFile, string targetFolder)
{
string remoteFile = sourceFile.Replace("\\","//");
string localFolder = targetFolder + "\\" + sourceFile.Substring(sourceFile.LastIndexOf("\\")+1);
string filename = "ftp://" + ftpServerIP + "//" + remoteFile;
FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;
ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftpReq.UseBinary = true;
ftpReq.Proxy = null;
ftpReq.KeepAlive = false; //'3. Settings and action
try
{
using (System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)(ftpReq.GetResponse()))
{
using (System.IO.Stream responseStream = response.GetResponseStream())
{
using (System.IO.FileStream fs = new System.IO.FileStream(localFolder, System.IO.FileMode.Create))
{
Byte[] buffer = new byte[2047];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
} while (read == 0);
responseStream.Close();
fs.Flush();
fs.Close();
}
responseStream.Close();
}
response.Close();
}
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
Console.Out.WriteLine(response.StatusDescription);
}
}
这太奇怪了:我按照http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21362579.html的说明操作,错误像烟火一样燃起,但是当过程完成时。 。所有文件都被下载了! ...?! ...我只是不明白 – Asaf 2010-09-02 06:08:20