.NET Framework 2.0 c#
I need to access a FTP Server. I'm using FTPWebRequest as described in visual studio 2005 documentation.
Everything works fine except the following:
1) I dont know how to change de current working directory on the server to the parent directory
2) Why cant I do this, or why doesn't work In Internet explorer works!
3) how can I send some FTP low levels commands to the server using .net 2.0 ftp api.
<some code...for FtpWebRequest>
FtpWebRequest ....Create("ftp://192.168.1.4/../ret");
thanks
Jose Cruz

FtpWebRequest
J2002Rogers
My appologiezes, I used the uri object to clarify the meaning but I see how it can be misleading.
I should have stated it this way: use the string directly in the FtpWebRequest
FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/%2E%2E/%2E%2E");
For the case where you say you only see your directory's name my guess is that you're in another directory (most likely one directory backwards) and it only contains your user's directory
Hope this helped
Mariya
wenliang
I'm tryint using this code...
FtpWebRequest
request = (FtpWebRequest)WebRequest.Create(ftp://192.168.0.2/../retorno);request.Credentials =
new NetworkCredential(username, password);request.Proxy =
null;request.UseBinary =
false;request.UsePassive =
true;request.Method =
WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = (FtpWebResponse)request.GetResponse();but the server returns me an error... problably because of "/../".
When I connect to server it puts me in some folder... but I want "../retorno"...
Shinya Watanabe
sorry.. this is not supported now. We MIGHT have a fully functional FTP client which maintains state(instead of the stateless webrequest response model we have now)...in the next release..
Thanks
Larkin Y
FtpWebRequest abstracts you away from things like changing directories. Say you want to download a file "a.txt in a subfolder "/foo/". You would create the following FtpWebRequest object:
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp:/server/foo/a.txt");
FtpWebResponse resp = (FtpWebResponse)req.GetResponse();
//read the response and then close it.
If you then wanted to download a file "b.txt" from a different folder "blah", then this is what you would do:
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp:/server/blah/b.txt");
FtpWebResponse resp = (FtpWebResponse)req.GetResponse();
//read the response and then close it.
Internally, FtpWebRequest will issue the change dir command and then request the file specified in the path. Basically, you always just give the path to the file you want and FtpWebRequest will change to the directory and issue the request.
deenaMay3
That would be great if MS didn't allow half baked API's to go into their framework. I suppose the FtpWebRequest sort of works, but it can't handle quotes (Good luck downloading extracts from mainframes) and doesn't know what to do with CWD commands.
kezzzs
cbretana
You should set the Credentials property of FtpWebRequest with the user name, password and domain. This is the right way to do things - else your username and password are easily seen on the wire
myFtpWebRequest.Credentials = new NetworkCredentilas("username", "password", "domain");
Mariya
Matthias R
I have written a blog on how you can navigate to different user's directories using FtpWebRequest
http://blogs.msdn.com/mariya/default.aspx
To directy answer the question just escape the dots in the url:
FtpWebRequest ....Create("ftp://192.168.1.4/%2E%2E/ret");
Maryam
This shouldn't happen. I am receiving the full list regardless of whether I use the slash at the end or not. This could be specific for the server you're using or it could be a bug on our part. Can you please send me a netmon trace and I'll have a look (Remove the funny chars and send it to mariya %$^&* at **) microsoft *%#! dot $ com
Thanks
Mariya
enpointeprolifix
Hello Mariya,
I suppose I wasn’t clear enough, so here is my code sample (try / catch sequences removed for simplicity)
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.
Create("ftp://myFtpUsername:myFtpPassword@myFtpServer");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
for (; ; )
{
string item = reader.ReadLine();
if (item == null)
return items;
Console.WriteLine(item);
}
reader.Close();
I get, for example, the following result in the console:
drwxrwxrwx 1 user user 1024 Mar 17 22:11 links
drwxrwxrwx 1 user user 512 Feb 05 2005 styles
drwxrwxrwx 1 user user 512 Jun 06 2004 tools
-rwxrwxrwx 1 user user 329 Feb 01 2004 index.html
If I replace the first statement by:
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.
Create("ftp://myFtpUsername:myFtpPassword@myFtpServer/links");
I get only one entry, which is the following:
drwxrwxrwx 1 user user 1024 Mar 17 22:11 links
But, in the same time I have been writing this note, I was running my sample to get a detailed response, and I got the idea which is the solution and give it to maybe help other users:
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.
Create("ftp://myFtpUsername:myFtpPassword@myFtpServer/links/");
By just adding a slash to the end of the address (isn’t it simple ), I got the content of the directory instead of the directory itself.
Best regards.
Phil_
Thank you for your e-mail and the captures.
Here is the FtpWebRequest behavior: when we have a slash at the end of the directory name we do:
cwd DirectoryName
LIST
In the case there is no slash at the end we do
LIST DirectoryName
According to RFC 959 LIST should return the list of files in both cases
LIST (LIST)
This command causes a list to be sent from the server to the
passive DTP. If the pathname specifies a directory or other
group of files, the server should transfer a list of files
in the specified directory. If the pathname specifies a
file then the server should send current information on the
file. A null argument implies the user's current working or
default directory.
In your case the problem occurs because your server is treating DirectoryName as filename and replies with file information.
That is a server problem, and the correct way to work around it (as you have already found out) is to have a slash at the end. This way we avoid giving DirectoryName as a parameter to LIST; we use a null argument instead.
Thanks
Mariya
swimmer
Well, it depends. First I need to know what is the error exactly.
To help clarify: Issuing (FtpWebRequest)WebRequest.Create(ftp://192.168.0.2/../retorno) is the same as issuing (FtpWebRequest)WebRequest.Create(ftp://192.168.0.2/retorno) and you'll be able or not able to this depending on the permissions your user has.
FtpWebRequest doesnt maintain state. If you've already listed one directory and then want to download something from the same directory you need to issue a new FtpWebRequest. If you want to change the directory and list the new directory you issue a new FtpWebRequest with a uri which is the new uri you want to list
Let me know if this helped
Mariya
devjam
How can I log in when username includes "@" like this...
username: user@domain.com"
password: pass
server: myFtpServer
Code would be like this:
FtpWebRequest
ftpRequestServer; ftpRequestServer = (FtpWebRequest)WebRequest.Create(ftp://user@domain.com:pass@myFtpServer/directory/);This fails because of the first "@". Is there any way to get around this
Ruben
Valent Lei
When I connect to server the ftp server puts me in a child directory!
I and to do a cwd .. to obtain the parent directory!
That's why I tried to use /../retorno, hoping that .net 2.0 do a cwd .. and then a cwd retorno.
But it was not the case.... :(