通常我用Java编程,但我想摆脱它。所以我在C#上做了个标记。虽然我注意到很多事情都很相似,但有些事情(显然)并不相似。
要解决我的问题:
im writing Basic程序下载视频文件并将其保存到用户指定的目录中。为了实现这一点,我在这里查看了Stackoverflow,很快就看到了解决方案。
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时,一切似乎都完美无瑕。当我按下按钮时,它会创建目录和第1集。mp4文件和打印
视频已定义!
但它也显示了一个消息框,上面写着“完成了!”过了一会儿,它会打印以下内容:
Der线程0x7e0 hat mit代码0(0x0)geendet-&燃气轮机;“线程0x7e0已停止,代码为0(0x0)。”
理论上,这应该下载文件。但事实并非如此。文件保持在0字节,没有网络活动。
你知道我怎样才能让它工作吗?