简体   繁体   中英

How to download a file to a specific path in from a given url in a windows form?

我需要使用winforms将指定链接(url)中的pdf文件下载到Windows应用程序中的特定文件夹,任何人都可以建议我使用解决方案。

using System.Net;

using (WebClient webClient = new WebClient())
{
    webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");
}

You can use the WebClient.DownloadFile method, available since .NET 2.0. It is usable from any type of application, not just Winforms.

You should be aware that DownloadFile blocks until the entire file finishes downloading. To avoid blocking you can use the WebClient.DownloadFileAsync method that will download in the background and raise the DownloadFileCompleted event when downloading finishes

You could just "search the web" (aka google) for "C# download file", and end up with this simple MSDN example (modified to fit your specific question):

string remoteUri = "http://www.test.com/somefile.pdf";
string fileName = "c:\\targetfolder\\somefile.pdf";

WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(remoteUri,fileName);
myWebClient.DownloadFile(myStringWebResource,fileName); 

If not the Target path is not specified and if you give it like file.abc it is downloaded to a path called Application.StartupPath as the name of file.abc So you just have to give your specific path like @"C:\\\\Folder1\\\\Folder2\\\\file.abc"

I think this will help a bit more. I could not get it at first site of sample codes provided by MSDN and at last i found this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM