Aru*_*mar 12 .net c# visual-studio-2010 winforms
我需要使用winforms将指定链接(url)中的pdf文件下载到Windows应用程序中的特定文件夹,任何人都可以建议我使用解决方案.
Vip*_*per 26
using System.Net;
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");
}
Run Code Online (Sandbox Code Playgroud)
您可以使用自.NET 2.0以来可用的WebClient.DownloadFile方法.它可以从任何类型的应用程序中使用,而不仅仅是Winforms.
您应该知道DownloadFile会阻塞,直到整个文件完成下载.为了避免阻塞,您可以使用将在后台下载的WebClient.DownloadFileAsync方法,并在下载完成时引发DownloadFileCompleted事件
您可以"搜索网络"(也称为谷歌)获取"C#下载文件",最后得到这个简单的MSDN示例(修改后适合您的具体问题):
string remoteUri = "http://www.test.com/somefile.pdf";
string fileName = "c:\\targetfolder\\somefile.pdf";
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(remoteUri,fileName);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57491 次 |
| 最近记录: |