WinSCP .NET ListDirectory seems to lock the directory
I'm connecting to an FTP server hosted by a public webhost and everything works fine (uploading and deleting files, creating and removing directories) until I use the
ListDirectory
command. I get the file listing but after that the FileExists
returns false and I'm not able to remove the directory. Closing and recreating the session makes the problem go away but that can't be intended? Connecting to the same FTP server with FileZilla works flawlessly.
public void TestSession() { var pathToDir = "path-to-existing-dir"; // This fails: second time FileExists is called it returns false var session = CreateSession(); var dirExists1 = session.FileExists(pathToDir); // true var dirListing = session.ListDirectory(pathToDir); var dirExists2 = session.FileExists(pathToDir); // false session.Close(); // This works: closing and recreating the session session = CreateSession(); var dirExists3 = session.FileExists(pathToDir); // true dirListing = session.ListDirectory(pathToDir); session.Close(); session = CreateSession(); var dirExists4 = session.FileExists(pathToDir); // true session.Close(); } private Session CreateSession() { var sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = _settings.Host, UserName = _settings.Username, Password = _settings.Password, PortNumber = _settings.Port }; var session = new Session(); session.Open(sessionOptions); return session; }