简体   繁体   中英

Get upload time of file uploaded at ftp server

Can anyone please tell me from where would i get the time of uploaded file at the FTP server?

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);
            }
        }
    }

try with FtpWebResponse.LastModified Property

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

you need to get responses for each file, in your current code you have already a list of files, by using that file name build full path to ftp file you need to find the create date. after that create ftp request to that full ftp file path. then you can read the last modified date of response object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM