在 WPF 应用程序中使用 URL 下载文件而无需打开浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24446269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:01:11  来源:igfitidea点击:

Download a file using URL in WPF Application without opening browser

c#wpfvisual-studio-2013

提问by Pruthvi Shetty

Is it possible to download a file using the download link URL using C# WPF Application without opening the browser? For example: The link - http://example.url.comwhen typed into the address bar of the browser automatically downloads a file. How can i download this file upon a button click in WPF application (C#) without opening the browser?

是否可以在不打开浏览器的情况下使用 C# WPF 应用程序使用下载链接 URL 下载文件?例如:链接 - http://example.url.com在浏览器的地址栏中输入时会自动下载文件。如何在不打开浏览器的情况下单击 WPF 应用程序 (C#) 中的按钮下载此文件?

TIA.

TIA。

回答by Yurii

You can use WebClient.DownloadFile, if you want to download a specific file and store it on your machine:

WebClient.DownloadFile如果您想下载特定文件并将其存储在您的机器上,您可以使用:

using (WebClient client = new WebClient())
{
    client.DownloadFile(remoteFilename, localFilename);
    ...
}

or WebClient.DownloadString, if you want the page's content as a string:

或者WebClient.DownloadString,如果您希望页面的内容为string

using (WebClient client = new WebClient())
{
    string reply = client.DownloadString (address);
    ...
}