How to change directories using FtpWebRequest?

Y

Yasir Zaheer

I need to access a FTP Server. I'm using FTPWebRequest as described in
visual studio 2005 documentation.
Everything works fine except i cannot change the current working
directory on the server to the parent directory.

I see the following methods listed in MSDN for the
WebRequestMethods.Ftp Members:

AppendFile Represents FTP APPE
DeleteFile Represents FTP DELE
DownloadFile Represents FTP RETR
GetDateTimestamp
GetFileSize Represents FTP SIZE
ListDirectory Represents FTP NLIST
ListDirectoryDetails Represents FTP LIST
MakeDirectory Represents FTP MKD
PrintWorkingDirectory Represents FTP PWD
RemoveDirectory Represents FTP RMD
Rename Represents FTP RENAME
UploadFile Represents FTP STOR
UploadFileWithUniqueName

When i am trying to pass "CDUP" FTP method to change the working
directory to parent it throws the argument exception

"This method is not supported. Parameter name: value"

this means that it is strictly allowing the set of methods present in
the enum. Is there any possiblilty to do this using .net libraries?
please, reply urgently.
 
K

Kevin Spencer

Have you tried using CWD?

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
M

Michael Nemtsev [MVP]

Hello Yasir,

Just give the path and FtpWebRequest will change the directory

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


YZ> I need to access a FTP Server. I'm using FTPWebRequest as described
YZ> in
YZ> visual studio 2005 documentation.
YZ> Everything works fine except i cannot change the current working
YZ> directory on the server to the parent directory.
YZ> I see the following methods listed in MSDN for the
YZ> WebRequestMethods.Ftp Members:
YZ>
YZ> AppendFile Represents FTP APPE
YZ> DeleteFile Represents FTP DELE
YZ> DownloadFile Represents FTP RETR
YZ> GetDateTimestamp
YZ> GetFileSize Represents FTP SIZE
YZ> ListDirectory Represents FTP NLIST
YZ> ListDirectoryDetails Represents FTP LIST
YZ> MakeDirectory Represents FTP MKD
YZ> PrintWorkingDirectory Represents FTP PWD
YZ> RemoveDirectory Represents FTP RMD
YZ> Rename Represents FTP RENAME
YZ> UploadFile Represents FTP STOR
YZ> UploadFileWithUniqueName
YZ> When i am trying to pass "CDUP" FTP method to change the working
YZ> directory to parent it throws the argument exception
YZ>
YZ> "This method is not supported. Parameter name: value"
YZ>
YZ> this means that it is strictly allowing the set of methods present
YZ> in the enum. Is there any possiblilty to do this using .net
YZ> libraries? please, reply urgently.
YZ>
 
Y

Yasir Zaheer

Thnx for considering Kevin,
Yup., i have tried "CWD directory name". but recieved the same error
"This method is not supported. Parameter name: value"
actually it do not accept any command which is not present in the .net
specified enumeration.
 
Y

Yasir Zaheer

Thnx for your suggestion Michael. you are right but actually my
problem is a bit different.

If i try to connect to a FTP site example "ftp.xxx.com", it routes my
directly to a child folder like "ftp.xxx.com/downloads/" but i want to
place some files in another location i.e "ftp.xxx.com/uploads/".

When i make an FTP request to upload a file "ftp://ftp.xxx.com/upload/
filename.txt" it throws an exception while
FtpWebRequest.GetRequestStream() >> "System.Net.WebException: The
remote server returned an error: (550) File unavailable (e.g., file
not found, no access)."

I looked at the FTP traffic b/w my application and site and observed
that the connection was a success but the PWD (Parent Working
Directory) was "/download/" and my since i gave a path "ftp://
ftp.xxx.com/upload/filename.txt" it then tries to change the directory
with the command "CWD /download/upload/", since no such directory
exists remote sever reutrned the error mentioned in the above
exeption.

I think you got my problem... do you have any solution for such a
case?

regards, Yasir Zaheer!
 
Y

Yasir Zaheer

Thnx for your suggestion Michael. you are right but actually my
problem is a bit different.

If i try to connect to a FTP site example "ftp.xxx.com", it routes my
directly to a child folder like "ftp.xxx.com/downloads/" but i want
to
place some files in another location i.e "ftp.xxx.com/uploads/".


When i make an FTP request to upload a file "ftp://ftp.xxx.com/
upload/
filename.txt" it throws an exception while
FtpWebRequest.GetRequestStream() >> "System.Net.WebException: The
remote server returned an error: (550) File unavailable (e.g., file
not found, no access)."


I looked at the FTP traffic b/w my application and site and observed
that the connection was a success but the PWD (Print Working
Directory) was "/download/" and my since i gave a path "ftp://
ftp.xxx.com/upload/filename.txt" it then tries to change the
directory
with the command "CWD /download/upload/", since no such directory
exists remote sever reutrned the error mentioned in the above
exeption.


I think you got my problem... do you have any solution for such a
case?


regards, Yasir Zaheer!
 
K

Kevin Spencer

Hi Yasir,

I understand. I found the FtpWebRequest class to be counterintuitive, as it
is modelled to behave like the HttpWebRequest, which is modelled after the
HTTP protocol, and that it hides too much information about the FTP message
exchange and network socket activity from the developer, so I created my own
FTP Client classes that are modelled along the lines of the actual FTP
protocol, rather than the HTTP protocol. Mine use sockets. So, I can't
really help you at this point, other than to understand your frustration.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
C

Chris Dunaway

Thnx for your suggestion Michael. you are right but actually my
problem is a bit different.

If i try to connect to a FTP site example "ftp.xxx.com", it routes my
directly to a child folder like "ftp.xxx.com/downloads/" but i want
to
place some files in another location i.e "ftp.xxx.com/uploads/".

When i make an FTP request to upload a file "ftp://ftp.xxx.com/
upload/
filename.txt" it throws an exception while
FtpWebRequest.GetRequestStream() >> "System.Net.WebException: The
remote server returned an error: (550) File unavailable (e.g., file
not found, no access)."

I looked at the FTP traffic b/w my application and site and observed
that the connection was a success but the PWD (Print Working
Directory) was "/download/" and my since i gave a path "ftp://
ftp.xxx.com/upload/filename.txt" it then tries to change the
directory
with the command "CWD /download/upload/", since no such directory
exists remote sever reutrned the error mentioned in the above
exeption.

I think you got my problem... do you have any solution for such a
case?

regards, Yasir Zaheer!

Does it support the use of wildcards? When you upload a file using
STOR, can you use a path like this: "../upload/filename.txt" where
the .. indicates a change to the parent directory?

Chris
 
Y

Yasir Zaheer

Hi Kevin,

Thanks for all your replies and understanding my feelings. I just
got a solution for that, actually there is a relative and absolute
path concept that .Net gave for these types of solutions. it uses a
"%2f" escape sequence that solves my problem. Using the address
www.ftp.abc.com/%2fuploads/filename.txt.

Regards.
 
Y

Yasir Zaheer

Hi Chris,

Thanks for your reply, I have tried the wild card ".." and "..."
earlier but the result was same. Actually they use the escape sequence
"%2f" as i have given the reply for the solution to Chris you can see.
in my previous message

Regards,
Yasir Zaheer.
 
A

Aref Alazzani

Hi Yasir,

When you create an ftp request with a path the request still remembers
the last created directory and use it as current. To avoid that use
request.KeepAlive = false after each use of ftp request.

I hope that this help.

Thanks,

Aref
 
K

kooka koo

Hi,
I have the similar question?
I can upload the file on root but i can't under the folder(FTP_TEST).
For example;
It works
string uri = "ftp://" + ftpServerIP + "//" + "TQ.bmp";

But it throws the error when i wanted to upload file in sub
directory(FTP_TEST).

string uri = "ftp://" + ftpServerIP + "//" + "FTP_TEST" + "//" +
"TQ.bmp";

Thanks,
 
K

kimiraikkonen

Hi,
I have the similar question?
I can upload the file on root but i can't under the folder(FTP_TEST).
For example;
It works
string uri = "ftp://" + ftpServerIP + "//" + "TQ.bmp";

But it throws the error when i wanted to upload file in sub
directory(FTP_TEST).

string uri = "ftp://" + ftpServerIP + "//" + "FTP_TEST" + "//" +
"TQ.bmp";

Thanks,

*** Sent via Developersdexhttp://www.developersdex.com***

Make sure directory exists.(FTP_TEST) and what exception you get is
somehow more important.

Thanks,

Onur Güzel
 
K

kooka koo

FTP_TEMP exists and the error message is
"{"The remote server returned an error: (550) File unavailable (e.g.,
file not found, no access)."}"

Thanks
 
K

kimiraikkonen

FTP_TEMP exists and the error message is
"{"The remote server returned an error: (550) File unavailable (e.g.,
file not found, no access)."}"

Thanks

*** Sent via Developersdexhttp://www.developersdex.com***

In your example you use FTP_TEST folder to upload file but in your
previous reply you stated FTP_TEMP? A typo?

Thanks,

Onur Güzel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top