C# 如何检查文件下载是否完成
我想做的是从网页下载一个文件。下载完文件后,我会让它在屏幕上打印一些东西C# 如何检查文件下载是否完成,c#,C#,我想做的是从网页下载一个文件。下载完文件后,我会让它在屏幕上打印一些东西 using HtmlAgilityPack; using NAudio.Wave; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threadin
using HtmlAgilityPack;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication25
{
class Program
{
static void Main(string[] args)
{
Uri remoteUri = new Uri("http://soundcloud.com/dubstep/spag-heddy-the-master-vip/download");
string fileName1 = "t", myStringWebResource = null;
// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
myStringWebResource = remoteUri + fileName1;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Url1_DownloadStringCompleted);
myWebClient.DownloadFileAsync(remoteUri, fileName1);
}
}
public static void Url1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
yes();
}
public static void yes()
{
Console.WriteLine("RRRR");
Console.Read();
}
}
}
我遇到的问题是myWebClient.DownloadFileAsync(remoteUri,fileName1)代码>我不确定那里应该有什么,而不是那里有什么。我还验证了
myWebClient.DownloadFile
是否有效。当我对代码进行这些更改时,工作正常:将输入字符串更改为URI,修复了本地路径,使用了正确的eventhandler并执行了控制台。最后阅读。我已经将代码缩短了一点,但您明白了:
static void Main(string[] args)
{
using (WebClient myWebClient = new WebClient())
{
myWebClient.DownloadFileCompleted += DownloadCompleted;
myWebClient.DownloadFileAsync(new Uri("http://someUrl"), @"e:\file.mp3");
}
Console.ReadLine();
}
public static void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Success");
}
当我对代码做了这些更改时,工作正常:将输入字符串更改为URI,修复了本地路径,使用了正确的eventhandler并执行了控制台。最后读取。我已经将代码缩短了一点,但您明白了:
static void Main(string[] args)
{
using (WebClient myWebClient = new WebClient())
{
myWebClient.DownloadFileCompleted += DownloadCompleted;
myWebClient.DownloadFileAsync(new Uri("http://someUrl"), @"e:\file.mp3");
}
Console.ReadLine();
}
public static void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Success");
}
好的,现在可以了,但只要我运行它,它就会打开和关闭。(控制台应用程序)如何让它触发下载完成事件?@MichaelLapan Put
console.ReadLine()
在myWebClient.DownloadFileAsync(remoteUri,fileName1);}代码>@Cyral这样做了,但仍然没有触发事件。我确实有足够的时间下载该文件。是的,这个功能是不够的。我下载图像,然后在此功能中编辑,但当我尝试加密该文件时,实际上文件没有完全下载,我的应用程序是crashingokay,现在可以工作了,但只要我运行它,它就会打开和关闭。(控制台应用程序)如何让它触发下载完成事件?@MichaelLapan Putconsole.ReadLine()
在myWebClient.DownloadFileAsync(remoteUri,fileName1);}代码>@Cyral这样做了,但仍然没有触发事件。我确实有足够的时间下载文件。是的,这个功能是不够的。我下载图像,然后在这个功能中编辑,但是当我试图加密这个文件时,实际上文件没有下载,我的应用程序崩溃了。你为什么不能用阻止下载文件的方法呢?@PaulK:同意,但我也知道有些环境中同步方法不起作用。为什么你不能使用阻止下载文件的方法呢?@PaulK:同意,但我也知道有些环境中同步方法不起作用。