C# DownloadFileAsync 不下载文件
C# DownloadFileAsync does not download the file
通常我在 Java 中编程,但想摆脱它。所以我选择了 C#。虽然我注意到很多东西都很安静,但有些不是(很明显)。
解决我的问题:
我正在编写的程序基本上是下载视频文件并将它们保存到用户指定的目录中。因此,为了实现这一点,我在 Whosebug 上查看并立即看到了解决方案。
WebClient.DownloadFileAsync(videoUri, saveDir);
所以我到处添加了一些代码,然后想到了这个:
private void btn_download_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(saveDir + a.HoleTitel());
WebClient webClient = new WebClient();
Uri video;
try
{
videoUri = new Uri(a.HoleVideoURL());
Console.WriteLine("Video has been defined! " + a.HoleVideoURL());
} catch
{
videoUri = null;
Console.WriteLine("Video is still null! NullPointerException incomming?");
}
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(videoUri, saveDir + a.HoleTitel() + "\Episode " + a.HoleEpisode() + ".mp4");
}
public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
prozess.Value = e.ProgressPercentage;
}
public void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Compleated!");
}
当我输入 Url 时,一切似乎都完美无缺。当我按下按钮时,它会创建 Directory 和 Episode 1.mp4 文件并打印
Video has been defined!
但它也显示了一个 MessageBox,说 "Compleated!" 片刻之后它打印了这个:
Der Thread 0x7e0 hat mit Code 0 (0x0) geendet. -> "The Thread 0x7e0 has stoped with Code 0 (0x0)."
理论上这应该下载文件。但它不会那样做。该文件保持在 0 字节,并且没有网络 activity。
你知道我怎样才能让它工作吗?
您的 DownloadFileAsync() 调用可能没问题。但是请注意,您已经以 Console.WriteLine("Video has been defined! " + a.HoleVideoURL());
的形式添加了一个很好的调试打印输出
你得到的输出是
Video has been defined!
这告诉你 a.HoleVideoURL() 不会给你任何 URL。因此下载立即完成,结果是一个空文件。
通常我在 Java 中编程,但想摆脱它。所以我选择了 C#。虽然我注意到很多东西都很安静,但有些不是(很明显)。
解决我的问题:
我正在编写的程序基本上是下载视频文件并将它们保存到用户指定的目录中。因此,为了实现这一点,我在 Whosebug 上查看并立即看到了解决方案。
WebClient.DownloadFileAsync(videoUri, saveDir);
所以我到处添加了一些代码,然后想到了这个:
private void btn_download_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(saveDir + a.HoleTitel());
WebClient webClient = new WebClient();
Uri video;
try
{
videoUri = new Uri(a.HoleVideoURL());
Console.WriteLine("Video has been defined! " + a.HoleVideoURL());
} catch
{
videoUri = null;
Console.WriteLine("Video is still null! NullPointerException incomming?");
}
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(videoUri, saveDir + a.HoleTitel() + "\Episode " + a.HoleEpisode() + ".mp4");
}
public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
prozess.Value = e.ProgressPercentage;
}
public void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Compleated!");
}
当我输入 Url 时,一切似乎都完美无缺。当我按下按钮时,它会创建 Directory 和 Episode 1.mp4 文件并打印
Video has been defined!
但它也显示了一个 MessageBox,说 "Compleated!" 片刻之后它打印了这个:
Der Thread 0x7e0 hat mit Code 0 (0x0) geendet. -> "The Thread 0x7e0 has stoped with Code 0 (0x0)."
理论上这应该下载文件。但它不会那样做。该文件保持在 0 字节,并且没有网络 activity。 你知道我怎样才能让它工作吗?
您的 DownloadFileAsync() 调用可能没问题。但是请注意,您已经以 Console.WriteLine("Video has been defined! " + a.HoleVideoURL());
你得到的输出是
Video has been defined!
这告诉你 a.HoleVideoURL() 不会给你任何 URL。因此下载立即完成,结果是一个空文件。