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 (12)

  1. ionutg says:

    This doesn’t seem to work for me in 2.0
    Any creation of a URI will execute a canonical transformation on the URI thus getting rid of .. or %2E2E
    Do I do something wrong here?

    WebRequest req = WebRequest.Create( @”https://somesite.com/../../../c:/WINDOWS/system32/cmd.exe” );
    OR
    WebRequest req = WebRequest.Create( @”https://somesite.com/%2E/%2E/%2E/c:/WINDOWS/system32/cmd.exe“ );
    try
    {
        Console.Write( “URL: t” + req.RequestUri );

    Can you give me some pointers please?

  2. Kaoticfen says:

    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

  3. I had a chance to play around with the FTP classes in .NET 2.0 (FtpWebRequest and FtpWebResponse). My

  4. grauenwolf says:

    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.

  5. This is a current compile of the team’s existing blogs on FtpWebRequest. I am going to update it periodically

  6. KunalPradhan says:

    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 !!!

  7. Janice says:

    Should that 550 error be happening "always" or "sometimes"?

    We are getting to "only sometimes".

    Should I try the %2F trick?

  8. John says:

    "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.

  9. Vinay says:

    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.

  10. Vinay says:

    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.

  11. Parab says:

    tried inplementing the same with %2F , didnt work.

    URI i used was

    ftp://myftpserveraddress:port/%2f/directory

    please help