I have to copy files to a particular directory. Before copying the files, I have to check if the directory already exists (if not then, create one).
To check the existence of a directory, I try to get the timestamp of that directory. If the directory exists then, I will get its timestamp and if not, then I will create a new directory.
My code to receive timestamp
// Try to get the LastModified date of the folder whose existence has to be checked
// Get the object used to communicate with the server.
string DirectoryPath = "ftp://66.220.9.50/FileDirectory";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(DirectoryPath));
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
request.Credentials = new NetworkCredential(_username, _password);
//Step-1: This line will decide if the Directory exists or not
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Status-1: " + response.StatusDescription);
Console.WriteLine("Last Modified: " + response.LastModified);
validDirectory = true;
response.Close();
PROBLEM: The above code works fine if I use another Windows PC as an FTP server (using FileZilla). But if I try to get the timestamp using above code from an online FTP server (wwww.driveHQ.com) then, the line FtpWebResponse response = (FtpWebResponse)request.GetResponse();
throws an exception:
The remote server returned an error: <550> File unavailable (e.g. file not found, no access)
PS: I am able to connect to the server (got status code: 150, Connection Accepted). My Uri
is also correct (I am successfully able to create a directory). The problem comes only if I try to get the timestamp of this created directory OR try to get a list of files inside the directory.