Jaa


Changing to the root directory with FtpWebRequest

Many customers ask us how they can use the CWD command with our FtpWebRequest.

The answer is: you cannot use the command directly, but you can modify the uri parameter to achieve the same result.

Let's say you're using the following format:

String uri = "ftp://myFtpUserName:myFtpUserPassword\@myFtpUrl";

FtpWebRequest Request = (FtpWebRequest)WebRequest.Create(uri);

Request.Method = "LIST";

The above example will bring you to your user's directory and list all the contents there. Now let's say you want to go 2 directories backwards and list the contents there (provided your user has permissions to do that). You close the previous FtpWebRequest and issue a new one with this uri

uri = "ftp://myFtpUserName:myFtpUserPassword\@myFtpUrl/%2E%2E/%2E%2E";

This is equivalent to logging in with your user's credentials and then using cd ../../

Note: if you try using the ”..”  directly without escaping them the uri class will strip them, so "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/../.." is equivalent to "ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/

Now let's say you want to go to another user's directory which is one level above the root. If you don't specify a user name and password it's equivalent to logging in as anonymous user. Then you issue a new FtpWebRequest with the following uri

"ftp://myFtpUrl/%2F/anotherUserDir"

This is equivalent to logging in as anonymous and then doing

Cd /

cd anotherUserDirectory

Comments

  • Anonymous
    July 06, 2006
    The comment has been removed

  • Anonymous
    July 13, 2006
    This is so incredibly helpful! thank you so much. I had spent a great deal of time trying to figure how to navigate to a different directory and I stumbled upon this. Thanks again

  • Anonymous
    July 27, 2006
    I had a chance to play around with the FTP classes in .NET 2.0 (FtpWebRequest and FtpWebResponse). My

  • Anonymous
    July 28, 2006
    Two things.

    One, this is the most aweful implmentation of a FTP client I have ever seen. You need to track down the architects and them senseless. Seriously, someone should have spoken up and said, "You know, trying to shoehorn a FTP client into the WebRequest model may not be a good idea."

    Secondly, please post this code on the MSDN Wki site.

  • Anonymous
    July 25, 2008
    This is a current compile of the team's existing blogs on FtpWebRequest. I am going to update it periodically

  • Anonymous
    March 13, 2009
    Hi, I am able to FTP a file using %2E from a console application. However when I try the same thing from Windows Service I am inable to FTP. It gives the below error The remote server returned an error: (550) File unavailable (e.g., file not found, no access). Are there any settings for Windows Service ? The code is the same in both the cases and also the Network Credentials. Please help !!!

  • Anonymous
    May 30, 2009
    PingBack from http://inverse-phone-directory.4pu.com/directory-backwards/30/

  • Anonymous
    November 20, 2011
    Should that 550 error be happening "always" or "sometimes"? We are getting to "only sometimes". Should I try the %2F trick?

  • Anonymous
    January 14, 2014
    "ftp://myFtpUrl/%2F/anotherUserDir" This is equivalent to logging in as anonymous and then doing Cd / cd anotherUserDirectory I need it to do the cd anotherUserDirectory without first doing the Cd /.  The Cd / is returning a 550 error on the remote server.

  • Anonymous
    March 27, 2014
    I am trying to do FTP to mainframe. Currently am able to upload and download the file. But the LRECL is set to 80 by default. I want to set this more, say about 1000. Could you please suggest on how i can construct the URI for this.

  • Anonymous
    March 27, 2014
    Further to this, see below on how I am building the URI for now. ftp://mysite/'filename' The file name is enclosed within single quote, because it is z/OS.

  • Anonymous
    April 16, 2014
    tried inplementing the same with %2F , didnt work. URI i used was ftp://myftpserveraddress:port/%2f/directory please help