我可以从C#下载ftp文件,但它会下载当前的日期文件。我需要将文件从上一个日期下载到当前日期。例如,我的文件是:
xys04-02-2011.zip xys04-03-2011.zip xys04-04-2011.zip
我在控制台应用程序中尝试以下代码:
string defaultLocalFolder_MCX = "C:\\";
FastZip fzMCX = new FastZip();
fzMCX.ExtractZip(
defaultLocalFolder_MCX + "\\xys03-31-2011.zip",
defaultLocalFolder_MCX, "");
FtpWebRequest requestFileDownload =
(FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDownload.Credentials = new NetworkCredential("test", "test");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse responseFileDownload =
(FtpWebResponse)requestFileDownload.GetResponse();
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
responseStream.Close();
writeStream.Close();
requestFileDownload = null;
responseFileDownload = null;
答案 0 :(得分:4)
嗯,听起来你需要按日期参数化你的方法,然后从那个日期生成文件名:
string formattedDate = date.ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);
fzMCX.ExtractZip(
defaultLocalFolder_MCX + "\\xys" + formattedDate + ".zip",
defaultLocalFolder_MCX, "");
然后只需在循环中调用该方法:
for (DateTime date = startDate; date <= dateTime.endDate; date = date.AddDays(1))
{
FetchZipFile(date);
}
(我只是猜到了这个名字,因为它不清楚zip文件提取是怎么回事。)
代码风格方面的一些建议:
using
语句,而不是手动关闭它们。包括WebResponses
。Stream.CopyTo