我正在使用FtpWebRequest
从 ftp 下载我的文件。但是,当应用程序转到FileStream
我声明写出文件的行时。
以下是我的下载功能:
public void Download(List <string> path)
{
try
{
string timenow = DateTime.Today.Year.ToString() + "_" + DateTime.Today.Month.ToString() + "_" + DateTime.Today.Day.ToString() + "_" + DateTime.Today.Hour.ToString() + "_" + DateTime.Today.Minute.ToString() + "_" + DateTime.Today.Second.ToString();
DirectoryInfo dir = new DirectoryInfo(@"C:\StudySystemFile\" + timenow);
if (!dir.Exists)
dir.Create();
foreach (string p in path)
{
FtpWebRequest request;
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp.Hostname + p));
request.Credentials = new System.Net.NetworkCredential(ftp.FtpAccount, ftp.FtpPassword);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.KeepAlive = false;
request.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream reader = response.GetResponseStream();
FileStream file = new FileStream(@"C:\StudySystemFile\" + timenow, FileMode.Create, FileAccess.ReadWrite);
byte[] buffer = new byte[1024];
int bytesRead = reader.Read(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
file.Write(buffer, 0, bytesRead);
bytesRead = reader.Read(buffer, 0, buffer.Length);
}
reader.Close();
file.Close();
response.Close();
Console.WriteLine(p);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}