Idan Zipris

Hi,

how can I copy files from a ftp account

I'm trying to do this:

and I'm getting this error:

"The given path's format is not supported."

Please help me!

Thanks, Idan.




Re: .NET Framework Networking and Communication Copy file from FTP

ahmedilyas

The File class does not support URL's as documented, it is only used for local copying of files.

You need to use the FtpWebRequest/FtpWebResponse classes to access FTP sites and perform FTP operations.

http://msdn2.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

http://msdn2.microsoft.com/en-us/library/system.net.ftpwebresponse.aspx

http://msdn2.microsoft.com/en-us/library/ms229711.aspx

The examples and documentation are pretty in depth and clear on what you can do and how to do it using the classes and hope this helps you.






Re: .NET Framework Networking and Communication Copy file from FTP

Idan Zipris

Thank you for your reply.

How can I declare the destination for the file I'm downloading






Re: .NET Framework Networking and Communication Copy file from FTP

ahmedilyas

Since the example shown is using a StreamReader to read the response stream, you then need to use the StreamWriter to write from that stream onto disk.

So...

using (StreamWriter writer = new StreamWriter("localPath\\filename.ext"))

{

writer.Write(responseStream.ReadToEnd());

}






Re: .NET Framework Networking and Communication Copy file from FTP

Idan Zipris

Ahmedilyas, I love you!

Thanks! it's not the first time you help me.






Re: .NET Framework Networking and Communication Copy file from FTP

ahmedilyas

Aww shucks :-)

No worries, glad I could help!






Re: .NET Framework Networking and Communication Copy file from FTP

Idan Zipris

Ahmedilyas, maybe you can help me with one more problem:

I would like to download all the files in some directory on FTP server to my computer.

I know how to download a file and I know how to find directory's contents (WebRequestMethods.Ftp.ListDirectory).

How can I download the files in the directory one by one I thought to do that with "foreach", to get all the files in a directory and then download them, but it's not succeed.

Thanks, Idan.






Re: .NET Framework Networking and Communication Copy file from FTP

ahmedilyas

What is the current code you have

Have you tried just giving it the directory to download, rather than the file itself What happens when you do this

Alternatively, you would need to get a list of files, and then for each file, download file.

The following method is not the best at all, but it does work. You need to make sure you dont download a directory as if you do, you will get an error.

Code Snippet

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://site.com/wwwroot/someDirectory");

System.Net.NetworkCredential creds = new NetworkCredential("username", "password");

request.Credentials = creds;

request.Method = WebRequestMethods.Ftp.ListDirectory;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

using (System.IO.Stream streamer = response.GetResponseStream())

{

using (System.IO.StreamReader reader = new System.IO.StreamReader(streamer))

{

string[] filesFoundInDirectory = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries );

foreach (string currentFile in filesFoundInDirectory)

{

FtpWebRequest downloadRequest = (FtpWebRequest)FtpWebRequest.Create(request.RequestUri.ToString() + "/" + currentFile);

downloadRequest.Credentials = creds;

downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse();

using (System.IO.Stream downloadStream = downloadResponse.GetResponseStream())

{

using (System.IO.StreamReader fileReader = new System.IO.StreamReader(downloadStream))

{

using (System.IO.StreamWriter fileWriter = new System.IO.StreamWriter(currentFile))

{

fileWriter.Write(fileReader.ReadToEnd());

fileWriter.Close();

}

}

}

}

}

}

Again, the code above is not the best at all, shouldn't really post it - bad practice however this is one method of doing what you are asking.

Should I, or any one else, find a better way - will post.






Re: .NET Framework Networking and Communication Copy file from FTP

Idan Zipris

When I tried to give the directory name, I got an error which said that I don't have an access to the directory.

I tried to write something like the method you gave me above, but there is a problem with this method, it's download files only, and if there is another directory inside the directory I declared, I get an error which says that I don't have an access (again).

Can I fix it somehow

Thank you very much for your help.






Re: .NET Framework Networking and Communication Copy file from FTP

ahmedilyas

Yes, this is what I stated in the previous message that you will have a problem if you are trying to download a directory which it thinks its a file.

I'm not sure at this time what I can suggest aside from getting a DirectoryListing, and doing some string parsing/splitting (which is very bad!) to see which items are a directory, then navigating to it and downloading the files.

I will see what else I can think of






Re: .NET Framework Networking and Communication Copy file from FTP

Idan Zipris

OK, thank you.






Re: .NET Framework Networking and Communication Copy file from FTP

Vinny79

Hi,

Actually i dont find the ftpwebrequest/ftpwebresponse classes in .NET 1.1 framework,so can you help me out how to copy a file in to a FTP server using .NET 1.1 framework.

Also i have one more question to add to this...if we try to copy a file in a FTP server in a particular folder which is password protected folder how to deal with it.

It will be of great help if someone can help me in this as it is high priority issue.

Regds,

Vinod






Re: .NET Framework Networking and Communication Copy file from FTP

ahmedilyas

I'm not sure about how to deal with a password protected folder. In regards to using it in .NET 1.1, I guess you would need to use the WebClient class and use the DownloadFile method.