System.Net.FtpWebRequest GetDateTimestamp example

后端 未结 3 461
花落未央
花落未央 2020-12-02 01:46

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

相关标签:
3条回答
  • 2020-12-02 02:21

    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);
    
    0 讨论(0)
  • 2020-12-02 02:31

    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);
    }
    
    0 讨论(0)
  • 2020-12-02 02:44

    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);
    }
    
    0 讨论(0)
提交回复
热议问题