I am trying to make a program that uploads/downloads .exe
file to a FTP
I tried using FtpWebRequest
, but I only succeed to upload and download .txt files.
Then i found here a solution for downloading using the WebClient
:
WebClient request = new WebClient();
request.Credentials = new NetworkCredential("username", "password");
byte[] fileData = request.DownloadData("ftp://myFTP.net/");
FileStream file = File.Create(destinatie);
file.Write(fileData, 0, fileData.Length);
file.Close();
This solution works. But I seen that WebClient
has a method DownloadFile
which did not worked. I think because it doesn't work on FTP
only on HTTP
. Is my assumption true? If not how can I get it to work?
And is there any other solution for uploading/downloading a .exe
file to ftp using FtpWebRequest
?
Uploading is easy...
Downloading is easy too... The code I made below uses BackgroundWorker (so it doesn't freeze the interface/application when download starts). Plus, I shortened the code with the use of lambda
(=>)
.You can also use DownloadFileAsync method to show real progress file downloading:
You need to say whether you're uploading text or binary files. Add the following line after request is declared & initialised:
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx
Both
WebClient.UploadFile
andWebClient.DownloadFile
work correctly for FTP as well as binary files.Upload
If you need a greater control, that
WebClient
does not offer (like TLS/SSL encryption, ascii/text transfer mode, etc), useFtpWebRequest
. Easy way is to just copy aFileStream
to FTP stream usingStream.CopyTo
:If you need to monitor an upload progress, you have to copy the contents by chunks yourself:
For GUI progress (WinForms
ProgressBar
), see:How can we show progress bar for upload with FtpWebRequest
If you want to upload all files from a folder, see
Upload directory of files using WebClient.
Download
If you need a greater control, that
WebClient
does not offer (like TLS/SSL encryption, ascii/text transfer mode, etc), useFtpWebRequest
. Easy way is to just copy an FTP response stream toFileStream
usingStream.CopyTo
:If you need to monitor a download progress, you have to copy the contents by chunks yourself:
For GUI progress (WinForms
ProgressBar
), see:FtpWebRequest FTP download with ProgressBar
If you want to download all files from a remote folder, see
C# Download all files and subdirectories through FTP.