Downloading and uploading to an FTP the easy way!
(Sep 08 2007 - 08:25:40 PM
by Timothy Khouri
) - [print blog
One of my biggest passions when it comes to programming has always been communication between computers (and networks), whether it be with raw communication with Sockets, Web Services, remoting or whatever. Just the thought of all that's going on behind the scenes to "connect" everyone together really blows my mind.
Along that line of thought is one of my favorite classes in the .NET framework, the WebClient class (in the System.Net namespace). I've always known of this class to upload and download files from a web server, but you can also use it for FTP servers too.
So without boring with too much talking, I'll show you how easy it is with some code:
WebClient myLilWebClient = new WebClient();
myLilWebClient.Credentials = new NetworkCredential("userName", "p@ssw0rd");
myLilWebClient.DownloadFile("ftp://myFtpAddress.com/somefile.txt", @"c:\documents and settings\tkhouri\desktop\somefile.txt");
I don't think it could get any easier than that! Oh, and if you wanted to Upload a file to an FTP... you'd simply call mYLilWebClient.UploadFile.
Gotta love .NET!