C#获取FTP文件的最后修改日期

前些时日,项目中用到了一些FTP操作。近日一朋友又问我如何获取FTP目录的最后修改日期,所以就总结一下。

获取FTP文件的最后修改日期,可用GetDateTimestamp方法,代码如下:

FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.free1.dns4u.cn/web/test.txt");
            req.Method = WebRequestMethods.Ftp.GetDateTimestamp;

            req.Credentials = new NetworkCredential("abc110", "123456");

            FtpWebResponse response = (FtpWebResponse)req.GetResponse();
            label1.Text = response.LastModified.ToString();
            response.Close();
            response = null;
            req = null;

但是这种方法,只能够获取文件的最后修改日期,如果是目,就会报错。

要得到文侠的最后修改日期,只能用ListDirectoryDetails方法,下代码是获取cert目录修改日期的

 FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.free1.dns4u.cn/web/");
            req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            req.Credentials = new NetworkCredential("abc110", "123456");

            FtpWebResponse response = (FtpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string str = "";
            do
            {
                str = reader.ReadLine();
                if (string.IsNullOrEmpty(str))
                    break;
                if(str.Substring(str.LastIndexOf(" ")).ToLower().Trim()=="cert")
                label1.Text += str + "\r\n";
            }
            while (!string.IsNullOrEmpty(str));
            reader.Close();
            response.Close();
            response = null;
            req = null;

 

出自:http://www.soft000.com/newsInfo.aspx?id=952d692b-da92-4f54-8540-0a6c68603970
上一篇:少上冈易(ZT)    下一篇:谢谢朋友的鼓励


评论人: 游客