System.Net.FtpWebRequest GetDateTimestamp example

2019-01-09 17:34发布

I'm looking for a short bit of sample code which uses the System.Net.FtpWebRequest namespace to get the timestamp of a specified remote file on an ftp server. I know I need to set the Method property of my request object to WebRequestMethods.Ftp.GetDateTimestamp but I'm not sure how to get the response back into a System.DateTime object.

3条回答
女痞
2楼-- · 2019-01-09 17:51

Something like this:

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);
}
查看更多
可以哭但决不认输i
3楼-- · 2019-01-09 17:52

To get the date field only but not the time, do exactly as the first answer in this thread with the following exception:

Console.WriteLine(response.LastModified().ToShortDateString);
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-09 17:57

Yep - thats pretty much what I ended up with. I went with something like this

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);
}
查看更多
登录 后发表回答