我正在尝试从互联网下载文件。我有一个if语句,否定子句会触发文件下载。这是我的代码:
if (!FileDownload)
{
}
else
{
using (WebClient wc = new WebClient())
{
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
wc.DownloadFileAsync(new Uri("https://www.google.com/Program.exe"),Application.StartupPath);
MessageBox.Show("Yes");
}
当我尝试运行程序时,出现以下错误:
错误1:
Could not copy "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Exceeded retry count of 10. Failed.
错误2:
Unable to copy file "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Could not find file 'obj\Debug\Program.exe'.
还有大约10个同样信息的警告:
Could not copy "obj\Debug\Program.exe" to "bin\Debug\Program.exe". Beginning retry 1 in 1000ms. Could not find file 'obj\Debug\Program.exe'.
然而,一旦我删除了if语句中的else子句,程序就可以执行而不出现任何错误。显然,else子句中的下载代码导致了问题,但我不确定原因。我曾在不同的事件中使用相同的代码,并且它能够完美地运行。感激任何帮助。
我已经尝试过:
- 关闭并重新打开Visual Studio
- 删除bin文件夹中的所有文件,然后重新构建。
- 使用Process Explorer检查文件是否被锁定
- 手动以管理员身份运行VS
- 删除bin和debug文件夹
- 重启计算机并将项目移动到其他文件夹