views:

1175

answers:

3

Is it possible to download a file from a website in Windows Application form and put it into a certain directory?

Thanks!

~~Seth

+9  A: 

Use WebClient.DownloadFile:

using (WebClient client = new WebClient())
{
    client.DownloadFile("http://csharpindepth.com/About.aspx", 
                        @"c:\Users\Jon\Test\foo.txt");
}
Jon Skeet
SKEEEEEEEEEEEEEET!
FlySwat
Don't forget IDisposable ;-p But (other than a "using") that is exactly what I came in to write...
Marc Gravell
Ah, normal service by the pedants is resumed ;-p
Marc Gravell
+3  A: 

Sure, you just use a HttpWebRequest.

Once you have the HttpWebRequest set up, you can save the response stream to a file streamwriter (Either binarystream, or a textwriter depending on the mimetype.) and you have a file on your harddrive.

EDIT: Forgot about WebClient. That works good unless as long as you only need to use GET to retrieve your file. If the site requires you to POST information to it, you'll have to use a HttpWebRequest, so I'm leaving my answer up.

FlySwat
+10  A: 

With the WebClient class:

using System.Net;
//...
WebClient Client = new WebClient ();
Client.DownloadFile("http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png", @"C:\folder\stackoverflowlogo.png");
CMS
Thank you, this worked exactly as I wanted!
S3THST4