Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change directories using FtpWebRequest (.NET)?

Can someone tell me how to change directories using FtpWebRequest? This seems like it should be an easy thing to do, but I'm not seeing it.

EDIT

I just want to add...I don't have my heart set on FtpWebRequest. If there's a better (easier) way to do FTP in .NET please let me know.


Apparently there's no way to do it using a live connection, you need to change the uri to trick ftpwebrequest into using a different request (thanks Jon).

So I'm looking for a 3rd party client...

Some of the open source solutions I tried didn't work too well (kept crashing), but I found one open source solution that's passed some of my preliminary tests (.NET FTP Client).

like image 668
Giovanni Galbo Avatar asked Dec 01 '08 06:12

Giovanni Galbo


2 Answers

There's a blog post from Mariya Atanasova which shows how you can fake it - basically you have to put the directory on the URL.

I suspect you may be better off with a dedicated FTP library though - one that doesn't try to force everything into the WebRequest way of doing things. I haven't personally used any 3rd party libraries for this, but a search for "FTP library .NET" finds lots of candidates.


Edit: jcolebrand (in case of 2006 blog linkrot possibility)

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
like image 74
Jon Skeet Avatar answered Nov 11 '22 14:11

Jon Skeet


You have to close the current connection:

request.Close();

And open a new one with an other uri:

uri = "ftp://example.com/%2F/directory" //Go to a forward directory (cd directory)
uri = "ftp://example.com/%2E%2E" //Go to the previously directory (cd ../)

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
like image 3
Giochi Blu Avatar answered Nov 11 '22 15:11

Giochi Blu