Welcome

Hello, Welcome to my blog. If you like feel free to refer others

Wednesday 8 April 2015

Last modified time of a file reside in FTP server

Add reference in your class:
using System.Net;
Then copy below mentioned method
 /// <summary>
        /// Get Server File TimeStamp
        /// </summary>
        /// <param name="serverPath"></param>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        ///  <param name="fileName"></param>
        /// <returns></returns>
        ///
        public static DateTime GetServerFileTimestamp(string serverPath, string userId, string password, string fileName)
        {
            // Get the object used to communicate with the server.
            String serverUrl = String.Format("{0}{1}/{2}", @"ftp:", serverPath, fileName);
            Uri serverUri = new Uri(serverUrl);
            FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(serverUrl);
            reqFTP.Method = WebRequestMethods.Ftp.GetDateTimestamp;           
            reqFTP.Credentials = new NetworkCredential(userId, password);
            try
            {
                using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
                {
                    // Return the last modified time.
                    return response.LastModified;
                }
            }
            catch (Exception ex)
            {
                // If the file doesn't exist, return min date Otherwise rethrow the error.
                if (ex.Message.Contains("File unavailable"))
                    return DateTime.MinValue;
                throw;
            }
            finally
            {
                reqFTP = null;               
            }
        }

Above mentioned code  will return you last modified time.

Happy learning.............

No comments:

Post a Comment