This is my code
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(FTPAddress);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
As you see, I am using ListDirectoryDetails
.
For every line in the directories
, this is the content:
ftp://172.28.4.7//12-22-14 01:21PM 9075 fileName.xml
My question is how to get the time from that line? Should I parse the string? I don't think so because I read that there is the LastModified
property, but I don't know how to use it.
Could you help me please?
Unfortunately, there's no really reliable and efficient way to retrieve modification timestamp of all files in a directory using features offered by .NET framework, as it does not support the FTP
MLSD
command. TheMLSD
command provides a listing of remote directory in a standardized machine-readable format. The command and the format is standardized by RFC 3659.Alternatives you can use, that are supported by .NET framework:
ListDirectoryDetails
method (the FTPLIST
command) to retrieve details of all files in a directory and then you deal with FTP server specific format of the detailsDOS/Windows format: C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response
*nix format: Parsing FtpWebRequest ListDirectoryDetails line
GetDateTimestamp
method (the FTPMDTM
command) to individually retrieve timestamps for each file. An advantage is that the response is standardized by RFC 3659 toYYYYMMDDHHMMSS[.sss]
. A disadvantage is that you have to send a separate request for each file, what can be quite inefficient. This method uses theLastModified
property that you mention:Alternatively you can use a 3rd party FTP client implementation that supports the modern
MLSD
command.For example WinSCP .NET assembly supports that.
(I'm the author of WinSCP)
Try using this code from MS documentation:
http://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.lastmodified%28v=vs.110%29.aspx
You should do this for each file. To do this, it is not simple either. You have to parse the result from your directory list response.
Check how this guy do that: Extracting file names from WebRequestMethods.Ftp.ListDirectoryDetails You should be able to do a foreach on each line read.