问题与FtpWebRequest getDateTimestamp和DateTime [英] Issue with FtpWebRequest getDateTimestamp and DateTime
问题描述
未处理的异常system.formatexception字符串未被识别为有效的日期时间
这是我用于将ftp文件创建日期转换为日期时间的代码
/ *获取创建文件的日期/时间* /
string fileDateTime = DownloadftpClient.getFileCreatedDateTime(test.txt);
Console.WriteLine(fileDateTime);
DateTime dateFromString = DateTime.Parse(fileDateTime,System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
Console.WriteLine(dateFromString);
任何关于我在这里做错的想法?
如果从服务器返回的字符串是 20121128194042
,那么你的代码需要是:
DateTime dateFromString = DateTime.ParseExact(fileDateTime,
yyyyMMddHHmmss,//指定收到的确切日期/时间格式
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();
返回ftpResponse.LastModified;
}
catch(Exception ex)
{
//不喜欢这样做,但我会在这里留下
//来保持与现有代码:
Console.WriteLine(ex.ToString());
}
返回DateTime.MinValue;
}
(从这个答案。)
然后调用代码变成:
DateTime lastModified = DownloadftpClient.getFileCreatedDateTime(test.txt);
I'm having a bit of trouble with a program that I'm writing. The program checks the date created of a file on a remote ftp site, then if it matched today's date it will download the file the upload it to another ftp site. I get the following error when I run the program:
unhandled exception system.formatexception string was not recognized as a valid datetime
Here is the code I'm using to convert the ftp file created date to a datetime
/* Get the Date/Time a File was Created */
string fileDateTime = DownloadftpClient.getFileCreatedDateTime("test.txt");
Console.WriteLine(fileDateTime);
DateTime dateFromString = DateTime.Parse(fileDateTime, System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
Console.WriteLine(dateFromString);
Any ideas on what I'm doing wrong here?
If the string returned from the sever is 20121128194042
, then your code needs to be:
DateTime dateFromString = DateTime.ParseExact(fileDateTime,
"yyyyMMddHHmmss", // Specify the exact date/time format received
System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
EDIT
The correct code for the getFileCreatedDateTime
method should be:
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;
}
(With help from this answer.)
The calling code then becomes:
DateTime lastModified = DownloadftpClient.getFileCreatedDateTime("test.txt");
这篇关于问题与FtpWebRequest getDateTimestamp和DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!