Thursday, March 3, 2011

How to download a file from a URL in C#?

What is a simple way of downloading a file from a URL path?

From stackoverflow
  • Have a look at System.Net.WebClient

  • Use System.Net.WebClient.Download file

    string remoteUri = "http://www.contoso.com/library/homepage/images/";
    string fileName = "ms-banner.gif", myStringWebResource = null;``
    
    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource,fileName);
    

    Original source MSDN, http://msdn.microsoft.com/en-us/library/ez801hhe.aspx

    Marc Gravell : WebClient is IDisposable - you should be "using" the myWebClient instance.
  • using (WebClient Client = new WebClient ())
    {
        Client.DownloadFile("http://www.abc.com/file/song/a.mpeg", "a.mpeg");
    }
    

0 comments:

Post a Comment