Mariya Atanasova's Blog

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

 

Published Monday, March 06, 2006 2:50 PM by mariya

Comments

 

ionutg said:

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?
July 6, 2006 7:49 PM
 

Kaoticfen said:

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
July 13, 2006 3:29 PM
 

Patrick Steele's .NET Blog said:

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

grauenwolf said:

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.
July 28, 2006 1:07 PM
 

Network Class Library Team (System.Net) said:

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

July 25, 2008 9:24 PM
Anonymous comments are disabled

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker