我使用C#Web應用程序,並需要使用FTP將文件下載到本地文件夾。這些圖像需要修改日期大於我指定的日期。將文件從C#中的FTP服務器下載到修改日期大於指定日期的本地文件夾中
代碼:
public static List<FTPLineResult> GetFilesListSortedByDate(string ftpPath, Regex nameRegex, DateTime cutoff, System.Security.Cryptography.X509Certificates.X509Certificate cert)
{
List<FTPLineResult> output = new List<FTPLineResult>();
if (cert != null)
{
FtpWebRequest request = FtpWebRequest.Create(ftpPath) as FtpWebRequest;
request.Credentials = new NetworkCredential("unm", "pwd");
request.ClientCertificates.Add(cert);
ConfigureProxy(request);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = request.GetResponse() as FtpWebResponse;
StreamReader directoryReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);
var parser = new FTPLineParser();
while (!directoryReader.EndOfStream)
{
var result = parser.Parse(directoryReader.ReadLine());
if (!result.IsDirectory && result.DateTime > cutoff && nameRegex.IsMatch(result.Name))
{
output.Add(result);
}
}
// need to ensure the files are sorted in ascending date order
output.Sort(
new Comparison<FTPLineResult>(
delegate(FTPLineResult res1, FTPLineResult res2)
{
return res1.DateTime.CompareTo(res2.DateTime);
}
)
);
}
return output;
}
我必須使用證書(或.p12)。
我該怎麼做?
感謝馬丁,我更新我的帖子的代碼我用來檢索文件列表。我可以在while塊中下載文件,而不是執行** output.Add(result); **我可以像**下載(destnationfilename,sourcefilename)**那樣做一些事情嗎? – dragy
當然可以。但是,如果這是您所期望的,我認爲這不會對性能有很大幫助。 –
我以爲我可以通過while循環傳遞相同的文件。在這種情況下,不需要通過過濾列表。但是,我不知道如何在現有的while循環中下載文件,並且使用另一個通過創建的列表來完成它。 – dragy