views:

110

answers:

1

I'm using FtpWebRequest to connect to an FTP server and I can use WebRequestMethods.Ftp.ListDirectoryDetails to list the directory details fine. However the response from the remote server has day, month and time but not year:

-rw-rw-rw- 1 user group 949 Jun 2 08:43 Unsubscribes_20100602.zip

-rw-rw-rw- 1 user group 1773 Jun 1 06:48 export_142571709.txt

-rw-rw-rw- 1 user group 1773 Jun 1 06:50 export_142571722.txt

-rw-rw-rw- 1 user group 980 Jun 1 06:51 export_142571734.txt

This is required for the application I'm writing so I tried to use WebRequestMethods.Ftp.GetDateTimestamp to get the datetimestamp for each file but the response is always empty. No exception is thrown.

try
{
    FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(path);

    ftp.Credentials = new NetworkCredential(_ftpUsername, _ftpPassword);
    ftp.Method = WebRequestMethods.Ftp.GetDateTimestamp;

    try
    {
        Stream stream = ftp.GetResponse().GetResponseStream();
        StreamReader sReader = new StreamReader(stream);

        return sReader;
    }
    catch (Exception exp)
    {
        throw new Exception(String.Format("An error occured getting the timestamp for {0}: {1}<br />", path, exp.Message));
    }
}

Has anyone got any ideas why this would be?

A: 

The GetDateTimestamp method does not return its data in the normal stream. Just like the file size method returns its data in the ContentLength header/property, the GetDateTimestamp method has its data in the LastModified header/property.

    FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(path);

    ftp.Credentials = new NetworkCredential(_ftpUsername, _ftpPassword);
    ftp.Method = WebRequestMethods.Ftp.GetDateTimestamp;

    try
    {
       using(FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
       {
           return response.LastModified;
       }
    }
    catch
    {
        throw new Exception(String.Format("An error occured getting the timestamp for {0}: {1}<br />", path, exp.Message));
    }

BTW You can check this answer as well.

ondesertverge