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

前端 未结 11 1330
清歌不尽
清歌不尽 2020-11-22 15:13

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

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 15:33

    Use System.Net.WebClient.DownloadFile:

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

提交回复
热议问题