在处理之前,我需要以编程方式下载一个大文件。最好的办法是什么?由于文件很大,我希望等待特定的时间,以便可以强制退出。
我知道webclient.downloadfile()。但似乎没有办法确定等待的具体时间,以便强行退出。
try
{
WebClient client = new WebClient();
Uri uri = new Uri(inputFileUrl);
client.DownloadFile(uri, outputFile);
}
catch (Exception ex)
{
throw;
}
另一种方法是使用命令行实用程序(wget)下载文件并使用processstartinfo启动命令,并使用process'waitforexit(int ms)强制退出。
ProcessStartInfo startInfo = new ProcessStartInfo();
//set startInfo object
try
{
using (Process exeProcess = Process.Start(startInfo))
{
//wait for time specified
exeProcess.WaitForExit(1000 * 60 * 60);//wait till 1m
//check if process has exited
if (!exeProcess.HasExited)
{
//kill process and throw ex
exeProcess.Kill();
throw new ApplicationException("Downloading timed out");
}
}
}
catch (Exception ex)
{
throw;
}
有更好的办法吗?请帮忙。谢谢。