我正在尝试使用DownloadFile该类的方法System.Net.WebClient从 GitHub 下载文件。我尝试.txt从这里下载文件,结果是这样的。这显然是页面的 HTML 代码,而不是文件。尝试谷歌搜索一些解决方案,但没有一个有效。这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace download_file_test
{
class Program
{
static void Main(string[] args)
{
using (WebClient wc = new WebClient())
{
wc.Headers.Add("a", "a");
try
{
wc.DownloadFile("https://github.com/github/platform-samples/blob/master/LICENSE.txt", @"C:/Users/User/Desktop/test/test.txt");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
Console.ReadLine();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)