Question

I have the following code to read items from my FTP-Server:

internal List<FtpItem> OpenFolder(string folderName)
{
   FtpWebRequest request = CreateFtpWebRequest("ftp://myserver.com", folderName);
   request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
   List<FtpItem> ftpItems = GetFtpItemsFromRequest(request);
   return ftpItems;
}

private List<FtpItem> GetFtpItemsFromRequest(FtpWebRequest ftpWebRequest)
{
   List<FtpItem> ftpItems = new List<FtpItem>();
   WebResponse webResponse = ftpWebRequest.GetResponse();
   StreamReader reader = new StreamReader(webResponse.GetResponseStream());
   string line = reader.ReadLine();
   while (line != null)
   {
      ftpItems.Add(new FtpItem(line));
      line = reader.ReadLine();
   }
   reader.Close();
   webResponse.Close();
   return ftpItems;
}   

private FtpWebRequest CreateFtpWebRequest(params string[] url)
{
   FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(string.Join("/", url));
   webRequest.KeepAlive = false;
   webRequest.UseBinary = true;
   webRequest.Credentials = this.networkCredential;
   return webRequest;
}

As long as I try to open a folder like root/sub/subsub everything works fine. But if the foldername contains a space, I get the error-message

No such file or directory

I tried to replace the space-character by %20, but this doesn't work.

What do I have to do to open folders which contains spaces?

Était-ce utile?

La solution

You forgot a closing "/" at the end of your url request. So either append it to the end of the request:

FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(string.Join("/", url)+"/");

or add an empty string as url parameter:

FtpWebRequest request = CreateFtpWebRequest("ftp://myserver.com", folderName,"");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top