3

I used a class for all my FTP transfers witch work fine in C# 3.5 but since I updaded to the framework 4, I have some problems.

I search on Google but find no solutions.

Especially with a method to check if a directory exists :

public bool DirectoryExists(string directory)
{
  bool directoryExists = false;
  if (directory.Substring(0, 1) != "/")
    directory = "/" + directory;
  FtpWebRequest request = GetFtpWebRequest(host + directory, WebRequestMethods.Ftp.PrintWorkingDirectory);
  try
  {
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    {
      directoryExists = true;
    }
  }
  catch (WebException)
  {
    directoryExists = false;
  }
  return directoryExists;
}

private FtpWebRequest GetFtpWebRequest(string url, string method)
{
  FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
  request.UseBinary = true;
  request.KeepAlive = true;
  request.UsePassive = (mode == Modes.Passive);
  request.Timeout = Timeout.Infinite;
  request.ServicePoint.ConnectionLimit = 6;
  request.ReadWriteTimeout = Timeout.Infinite;
  if (credential == null)
    credential = new NetworkCredential(login, password);
  request.Credentials = credential;
  request.Method = method;
  return request;
}

The method DirectoryExists always return true (even the directory does not exist) but only on the framework 4, before a exception was thrown by GetFtpWebRequest if the directory does not exists.

Does anyone had this problem ?

Please don't tell me to use an other library cause all my programs depend of this one and I don't want to update all ...

A.Baudouin
  • 2,855
  • 3
  • 24
  • 28
  • 1
    Have you tried checking the contents of the response before returning TRUE; – Panagiotis Kanavos Jan 31 '11 at 14:21
  • 1
    Did you update your project to .NET 4.0? If so, check that you're using the .NET 4.0 Framework rather than the .NET 4.0 Client Profile in the project properties. This has fixed about 75% of all of the compatibility issues I've run into so far. – Weegee Jan 31 '11 at 14:23
  • Yes I update the project to ".NET framework 4". If I use ".NET framework 3.5" it's ok. On ".NET framework 4", the response StatusCode is always "PathnameCreated" if the directory exists or not ... – A.Baudouin Jan 31 '11 at 14:28
  • After many tests, it appears that with the framework 4, the PrintWorkingDirectory method always return "/" (if the directory exists or not). My FTP server is on Ubuntu ... – A.Baudouin Jan 31 '11 at 16:29

2 Answers2

2

Just change:

WebRequestMethods.Ftp.PrintWorkingDirectory

to ...

WebRequestMethods.Ftp.ListDirectory

and your code will work fine in .NET 4.0.

DJGrizz
  • 21
  • 1
  • Yes, this is what i have done. But it's take about 20x more times to make this test and this a real problem for my FTP applications ... – A.Baudouin Feb 07 '11 at 12:56
1

The problem is that in new implementation (4.0) client does not send command 'CWD' . Use method SetMethodRequiresCWD() from here microsoft RESOLUTION https://support.microsoft.com/en-us/kb/2134299

user3190933
  • 146
  • 1
  • 3