0
好吧,我试着用WebClient的C#类到donwload从GitHub一个文件,但我总是损坏的文件..这是我的代码C#下载文件损坏
using (var client = new WebClient())
{
client.DownloadFile("https://github.com/trapped/rotmg_svr/archive/master.zip", @"C:/Users/Asus/Desktop/aa.zip");
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
}
static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage.ToString());
}
//////
public static void ReadFile()
{
WebClient client = new WebClient();
client.DownloadFile("https://github.com/trapped/rotmg_svr/archive/master.zip", @"C:/Users/Asus/Desktop/aa.zip");
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(client_DownloadFileCompleted);
}
static void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Console.WriteLine("Finish");
}
static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage);
}
现在我使用该代码并调用该函数Reader.ReadFile(); ,文件下载好,但没有任何东西写在控制台(e.percentage)。 谢谢
当您手动下载文件(无代码)时,该文件是否正常? – John
是的,将尝试JustAnotherUser回答 –
您应该使用'WebClient.DownloadFileAsync()'而不是'WebClient.DownloadFile()' –