1

谁能告诉我从哪里可以得到上传文件在 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);
            }
        }
    }
4

1 回答 1

0

尝试使用FtpWebResponse.LastModified属性

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

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

于 2013-04-15T05:35:33.843 回答