none
How to get a file's date before downloading the ftp file RRS feed

  • Question

  • Group

           I am in a project where we are downloading the file from an ftp without any problem, only that it was asked to get the date that the file was made available on the ftp, for internal control purposes, in this case how I would be able to do this part to obtain the date that the file was made available within ftp?

          Where I am sending part of the code to get a sense of how it has been done so far.
    conect(origem);
                    ftp.Method = WebRequestMethods.Ftp.DownloadFile;
    
                    using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
                    {
                        using (Stream rs = response.GetResponseStream())
                        {
                            using (FileStream ws = new FileStream(destinoLocal, FileMode.Create))
                            {
                                byte[] buffer = new byte[2048];
                                int bytesRead = rs.Read(buffer, 0, buffer.Length);
                                while (bytesRead > 0)
                                {
                                    ws.Write(buffer, 0, bytesRead);
                                    bytesRead = rs.Read(buffer, 0, buffer.Length);
                                }
                            }
                        }
                    }
    
                    ftp.Abort();
    

    Wednesday, March 4, 2020 1:44 PM

Answers

  • A test with a random file =>

    Uri uriFTPFile = new Uri("ftp://ftp.intel.com/readme.txt");
    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uriFTPFile);
    ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
    DateTime dtLastModified = ftpResponse.LastModified;
    Console.WriteLine("Last Modified Date : {0}", dtLastModified.ToString());

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Wednesday, March 4, 2020 2:06 PM
  • Hi beibala,

    Thank you for posting here.

    C # has a class FileInfo that can modify the last modified time of a file.

    So, since you can get the original time of the file, you should be able to use FileInfo to modify it after the download is complete.

    Try the following code.

                ftp.Method = WebRequestMethods.Ftp.DownloadFile;
    
                using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
                {
                    DateTime dateTime = response.LastModified;
                    using (Stream rs = response.GetResponseStream())
                    {
                        using (FileStream ws = new FileStream(destinoLocal, FileMode.Create))
                        {
                            FileInfo fileInfo = new FileInfo(destinoLocal);
                            fileInfo.LastWriteTime = dateTime;
    
                            byte[] buffer = new byte[2048];
                            int bytesRead = rs.Read(buffer, 0, buffer.Length);
                            while (bytesRead > 0)
                            {
                                ws.Write(buffer, 0, bytesRead);
                                bytesRead = rs.Read(buffer, 0, buffer.Length);
                            }
                        }
                    }
                }
    
                ftp.Abort();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Thursday, March 5, 2020 3:39 AM
  • It's always going to take two transactions: one to get the timestamp, one to get the file.  It's exactly like having a human do this task.  You would log in, do a "dir" to check the timestamp, then do a "get" to fetch the fie.  FTP was designed for human use; these APIs are emulating a human being.

    It takes almost no time to fetch the timestamp, so I don't know why you're hesitant to do that.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Monday, March 9, 2020 5:26 PM
  • // Use FTP to get a remote file's timestamp.
    private DateTime FtpGetFileTimestamp(string uri, string user_name,
        string password)
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
        request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    
        // Get network credentials.
        request.Credentials =
            new NetworkCredential(user_name, password);
    
        try
        {
            using (FtpWebResponse response =
                (FtpWebResponse)request.GetResponse())
            {
                // Return the size.
                return response.LastModified;
            }
        }
        catch (Exception ex)
        {
            // If the file doesn't exist, return Jan 1, 3000.
            // Otherwise rethrow the error.
            if (ex.Message.Contains("File unavailable"))
                return new DateTime(3000, 1, 1);
            throw;
        }
    }

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Thursday, March 5, 2020 7:36 AM

All replies

  • A test with a random file =>

    Uri uriFTPFile = new Uri("ftp://ftp.intel.com/readme.txt");
    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uriFTPFile);
    ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
    DateTime dtLastModified = ftpResponse.LastModified;
    Console.WriteLine("Last Modified Date : {0}", dtLastModified.ToString());

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Wednesday, March 4, 2020 2:06 PM
  •    Group / Castorix31

              I am checking with the staff here and it was commented that the files are being downloaded all at the same time and that sometimes the date is changed, due to the file being created in the ftp download, according to the code I sent, in this case based on the code I sent and your code, would you be able to show me the best placement of your code within the code I sent? Where the case really changes the date, I was thinking of taking it at the beginning of the file and transferring it to another property and in the end taking this date to another property, there would be something in this sense that I would be able to do, based on your code did you send me?

    Wednesday, March 4, 2020 2:56 PM
  • Hi beibala,

    Thank you for posting here.

    C # has a class FileInfo that can modify the last modified time of a file.

    So, since you can get the original time of the file, you should be able to use FileInfo to modify it after the download is complete.

    Try the following code.

                ftp.Method = WebRequestMethods.Ftp.DownloadFile;
    
                using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
                {
                    DateTime dateTime = response.LastModified;
                    using (Stream rs = response.GetResponseStream())
                    {
                        using (FileStream ws = new FileStream(destinoLocal, FileMode.Create))
                        {
                            FileInfo fileInfo = new FileInfo(destinoLocal);
                            fileInfo.LastWriteTime = dateTime;
    
                            byte[] buffer = new byte[2048];
                            int bytesRead = rs.Read(buffer, 0, buffer.Length);
                            while (bytesRead > 0)
                            {
                                ws.Write(buffer, 0, bytesRead);
                                bytesRead = rs.Read(buffer, 0, buffer.Length);
                            }
                        }
                    }
                }
    
                ftp.Abort();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Thursday, March 5, 2020 3:39 AM
  • // Use FTP to get a remote file's timestamp.
    private DateTime FtpGetFileTimestamp(string uri, string user_name,
        string password)
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
        request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    
        // Get network credentials.
        request.Credentials =
            new NetworkCredential(user_name, password);
    
        try
        {
            using (FtpWebResponse response =
                (FtpWebResponse)request.GetResponse())
            {
                // Return the size.
                return response.LastModified;
            }
        }
        catch (Exception ex)
        {
            // If the file doesn't exist, return Jan 1, 3000.
            // Otherwise rethrow the error.
            if (ex.Message.Contains("File unavailable"))
                return new DateTime(3000, 1, 1);
            throw;
        }
    }

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Thursday, March 5, 2020 7:36 AM
  • Timon Yang / Group

    I was analyzing the code and a question arose, would I really need to have this part of the code or not (part 1a), due to some basic structure or necessary to have the date of the ftp file?
        Because this structure downloads several files at the same time, it was happening that the dates of the file are coming like this ({01/01/0001 00:00:00}), so the staff ended up making 2 connections to make sure to pay the date correctly? Where it was even commented if it would not be due to be using the instruction (part 2a). So we were hoping it could work within a single code.

    (part 1a)

    ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;

    (part 2a)

    ftp.Method = WebRequestMethods.Ftp.DownloadFile

    Thursday, March 5, 2020 9:00 PM
  • Hi neibala,

    I tried to find such a method, but so far I haven't found it.

    Obtaining time and downloading files are two tasks, and a two-step execution may be necessary.

    Update: I found this but I haven't tried it yet, maybe it is helpful.

    Download files from FTP server in C# to local folder with modification date greater than specified

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, March 9, 2020 9:14 AM
  • It's always going to take two transactions: one to get the timestamp, one to get the file.  It's exactly like having a human do this task.  You would log in, do a "dir" to check the timestamp, then do a "get" to fetch the fie.  FTP was designed for human use; these APIs are emulating a human being.

    It takes almost no time to fetch the timestamp, so I don't know why you're hesitant to do that.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by neibala Thursday, March 12, 2020 6:55 PM
    Monday, March 9, 2020 5:26 PM
  • Timon Yang / Tim Roberts / Group

    People seeing the explanations and tips sent by both (Timon Yang and Tim Roberts), it became much clearer how the process should be and that the time is really fast, because the biggest question that remained afterwards was whether or not and seeing the explanations it was very clear that it really had to have 2 processes to get what was expected and this is how I can meet 100%, because it worked here in our tests and I just have to thank you for your attention and help

    Thursday, March 12, 2020 6:54 PM