我正在寻找它使用System.Net.FtpWebRequest命名空间来获得指定的远程文件的时间戳的FTP服务器上的示例代码短位。我知道我需要设置我的请求对象WebRequestMethods.Ftp.GetDateTimestamp的方法属性,但我不知道怎么去响应返回到System.DateTime的对象。

有帮助吗?

解决方案

是的 - 这就是差不多就是我结束了。我有这样的事情去了。

request = FtpWebRequest.Create("ftp://ftp.whatever.com/somefile.txt");

request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
request.Proxy = null;

using (FtpWebResponse resp = (FtpWebResponse)request.GetResponse())
{
        Console.WriteLine(resp.LastModified);
}

其他提示

要获取日期字段只,但还不是时候,正好做在这个帖子下面的异常的第一个答案:

Console.WriteLine(response.LastModified().ToShortDateString);

像这样:

DateTime DateValue;    

FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(yourUri);
Request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
Request.UseBinary = false;

using (FtpWebResponse Response = (FtpWebResponse)Request.GetResponse())
using (TextReader Reader = new StringReader(Response.StatusDescription))
{
    string DateString = Reader.ReadLine().Substring(4);
    DateValue = DateTime.ParseExact(DateString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture.DateTimeFormat);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top