C#从没有扩展名的URL下载文件

时间:2016-02-22 14:19:06

标签: c# file

我想通过下载网页从网站获取数据,但是当我把这个链接“http:\ www.championcounter.com \ Jinx”作为此代码的参数时:

string url = "http:\\www.championcounter.com\Jinx";
string nome = "C:\Temp\data.dat";
WebClient webClient = new WebClient();
webClient.DownloadFile(url, nome );

它给了我这个错误:

System.UriFormatException: URI non valido: impossibile determinare il formato dell'URI.
in System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
in System.Uri..ctor(String uriString)
in LoLCounter.Form.download(String url, String nome) in   c:\users\filippo\documents\visual studio 2015\Projects\LoLCounter\LoLCounter\Form1.cs:riga 39

我认为这是因为它找不到网站,因为在网址中没有任何扩展名

1 个答案:

答案 0 :(得分:2)

using System.Net;

您可以使用:

var url = "http://www.championcounter.com/Jinx";

var textFromFile = (new WebClient()).DownloadString(url); // get as string

(new WebClient()).DownloadFile(url, "D:\\pathToFile"); // or save to file