如果从服务器返回的字符串是20121128194042
,那么您的代码需要是:
DateTime dateFromString = DateTime.ParseExact(fileDateTime,
"yyyyMMddHHmmss", // Specify the exact date/time format received
System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
编辑
该getFileCreatedDateTime
方法的正确代码应该是:
public DateTime getFileCreatedDateTime(string fileName)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
return ftpResponse.LastModified;
}
catch (Exception ex)
{
// Don't like doing this, but I'll leave it here
// to maintain compatability with the existing code:
Console.WriteLine(ex.ToString());
}
return DateTime.MinValue;
}
(在这个答案的帮助下。)
然后调用代码变为:
DateTime lastModified = DownloadftpClient.getFileCreatedDateTime("test.txt");