There is a code. In my opinion it is completely correct.

public static void MyFTP(string localFilePath, string ftpFileName) { using(FtpConnection ftp = new FtpConnection("87.237.43.11", "123", "123")) { ftp.Open(); /* Open the FTP connection */ ftp.Login(); /* Login using previously provided credentials */ string yearmonth = localFilePath.Substring(25, 6); MessageBox.Show(yearmonth); if (ftp.DirectoryExists("/archive/browse/RESURSDK/" + yearmonth)) /* check that a directory exists */ ftp.SetCurrentDirectory("/archive/browse/RESURSDK/" + yearmonth); /* change current directory */ else ftp.CreateDirectory("/archive/browse/RESURSDK/" + yearmonth); //do some processing try { ftp.SetCurrentDirectory("/archive/browse/RESURSDK/" + yearmonth); ftp.PutFile(localFilePath, ftpFileName); } catch (FtpException e) { Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message)); } foreach(var dir in ftp.GetDirectories("/www/")) { Console.WriteLine(dir.Name); Console.WriteLine(dir.CreationTime); foreach(var file in dir.GetFiles()) { Console.WriteLine(file.Name); Console.WriteLine(file.LastAccessTime); } } 

However the error takes off

http://postimage.org/image/b6jksqhz9/

  • class FtpConnection from which library? - Ivan Navoznov
  • 2
    The code is straight and asks for refactoring. - Ivan Navoznov
  • one
    Oh God, you have FTP operations in the UI stream. And you say your code is correct ? - VladD
  • And you yourself would see if there is such a directory or not, otherwise you have to wait for the telepaths to catch up. - VladD

2 answers 2

To you, the message is black by ... um ... beige writes: "directory exists". You call the CreateDirectory method, and pass it the name of an already existing directory. He is trying to create it, and it already exists. Hence the error.

  • I think his question is why the test (create only if it does not exist) did not work. - Ivan Navoznov
  • [Here] [1] source of the library, which he used. In the CreateDirectory method there are only three lines whose main task is to call a function from WinAPI. Hence the conclusion: either cant in WinAPI (which is unlikely), or the folder really is on the server (which is most likely). [1]: ftplib.codeplex.com/SourceControl/changeset/view/82946#589935 - fori1ton

Here is the CreateDirectory method:

 /// <summary> /// Creates a directory on the FTP server. /// </summary> /// <param name="path">A <see cref="String"/> representing the full or relative path of the directory to create.</param> public void CreateDirectory(string path) { if (WININET.FtpCreateDirectory(_hConnect, path) == 0) { Error(); } } 

In the comments it is written that you need to specify either the full path or relative. You use the second method, you want to specify a relative path. But I suspect that the path should be specified relative to the current folder, and not the root folder (root folder).

Tips for solving the problem: try to specify the full path of the folder in the parameter CreateDirectory.