c# - C# 没有下载
问题描述
我在通过 C# 下载文件时遇到问题。
我有一个类可以像这样处理下载:
namespace Ultra_Script
{
class FileDownloader
{
private readonly string _url;
private readonly string _fullPathWheretoSave;
private bool _result = false;
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);
public FileDownloader(string url, string fullPathWheretoSave)
{
if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");
this._url = url;
this._fullPathWheretoSave = fullPathWheretoSave;
}
public bool StartDownload(int timeout)
{
try
{
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));
if (File.Exists(_fullPathWheretoSave))
{
File.Delete(_fullPathWheretoSave);
}
using (WebClient client = new WebClient())
{
var ur = new Uri(_url);
//client.Credentials = new NetworkCredential("username", "password");
client.DownloadProgressChanged += WebClientDownloadProgressChanged;
client.DownloadFileCompleted += WebClientDownloadCompleted;
Console.WriteLine(@"Downloading File:");
client.DownloadFileAsync(ur, _fullPathWheretoSave);
_semaphore.Wait(timeout);
return _result && File.Exists(_fullPathWheretoSave);
}
}
catch (Exception e)
{
Console.WriteLine("Cant download file");
Console.Write(e);
return false;
}
finally
{
this._semaphore.Dispose();
}
}
private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.Write("/r --> {0}%", e.ProgressPercentage);
}
private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)
{
_result = !args.Cancelled;
if (!_result)
{
Console.Write(args.Error.ToString());
}
Console.WriteLine(Environment.NewLine + "Download Finished!");
_semaphore.Release();
}
public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)
{
return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);
}
}
}
我这样称呼它:
public static void InstallBasicSW()
{
var succes = FileDownloader.DownloadFile("https://github.com/Corbieman/Basic_SW/raw/master/JaVa.exe", "C:\\Windows", 99999999);
Console.WriteLine("Done - Succes: " + succes);
Console.ReadLine();
}
但只有我在控制台中得到的是:
下载完成!
完成 - 成功:错误;
我没有收到任何错误消息或进度条。这条消息会立即弹出。并且文件不会下载到该路径中。有人知道或知道问题出在哪里吗?
解决方案
方法 DownloadFile 的参数需要完整文件的路径。
尝试这个 :
public static void InstallBasicSW()
{
var succes = FileDownloader.DownloadFile("https://github.com/Corbieman/Basic_SW/raw/master/JaVa.exe", @"C:\Temps\JaVa.exe", 99999999);
Console.WriteLine("Done - Succes: " + succes);
Console.ReadLine();
}
为什么你有这个结果?我认为这是因为您传递的是目录路径而不是文件路径。下载取消并立即完成。
一个明确的例外会更有帮助......
推荐阅读
- qt - Qt 将来自多个发送者的信号映射到单个插槽
- neo4j - Neo4j Cypher 仅返回计数大于 1 的行
- pdf - 使用 VB 脚本将 Excel 工作簿中的活动工作表保存为 PDF 文件
- jira - JIRA - 当所有状态都可以进入特定状态时,删除到相同状态的转换
- rstudio - R-Studio - 与 Teradata 的连接不起作用
- installation - 为什么我的非常简单的 .exe 在任何地方都被标记为危险?
- browser - 即使用不同的字体书写,计算机如何解释/理解或识别特定的字母?
- javascript - 如何获取 HTML 的“for=”属性
- windows - Windows 10 上 Docker-Desktop 的 Hyper-V 和数据执行错误消息无法启动
- uml - 如何在 UML 中引用包含其类名的操作?