获取上传到ftp服务器的文件的上传时间

时间:2013-04-15 05:03:42

标签: c# ftp

任何人都可以告诉我,从哪里可以获得FTP服务器上传文件的时间?

class listFiles
{
    public static void Main(string[] args)
    {
        listFiles l = new listFiles();
        l.getFileList(ftpConnection,"test123","pass123"); //ftp url
    }
    private void getFileList(string FTPAddress, string username, string password)
    {
        List<string> files = new List<string>();

        try
        {
            //Create FTP request
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress);

            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = true;


            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            while (!reader.EndOfStream)
            {
                //Application.DoEvents();
               // string fileType = reader.ReadLine();
                files.Add(reader.ReadLine());
            }

            //Clean-up
            reader.Close();
            responseStream.Close(); //redundant
            response.Close();
        }
        catch (Exception)
        {
            Console.WriteLine("There was an error connecting to the FTP Server");
        }

        //If the list was successfully received, display it to the user
        //through a dialog
        if (files.Count != 0)
        {
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用FtpWebResponse.LastModified Property

using (FtpWebResponse resp = (FtpWebResponse)request.GetResponse())
{
     var lastModified =   resp.LastModified ;
}

您需要获取每个文件的响应,在您当前的代码中已经有一个文件列表,通过使用该文件名构建ftp文件的完整路径,您需要找到创建日期。之后,为该完整的ftp文件路径创建ftp请求。然后你可以阅读响应对象的最后修改日期。