C# 等待文件通过webClient从URL下载

C# 等待文件通过webClient从URL下载,c#,webclient-download,C#,Webclient Download,我很难从URL下载几MB的excel文件,然后再使用它。我使用VS2010,所以我不能使用等待关键字。 我的代码如下: using (WebClient webClient = new WebClient()) { // setting Windows Authentication webClient.UseDefaultCredentials = true;

我很难从URL下载几MB的excel文件,然后再使用它。我使用VS2010,所以我不能使用等待关键字。 我的代码如下:

using (WebClient webClient = new WebClient())
                {
                    // setting Windows Authentication
                    webClient.UseDefaultCredentials = true;
                    // event fired ExcelToCsv after file is downloaded
                    webClient.DownloadFileCompleted += (sender, e) => ExcelToCsv(fileName);
                    // start download
                    webClient.DownloadFileAsync(new Uri("http://serverx/something/Export.ashx"), exportPath);
                }
exceltosv()方法中的行

using (FileStream stream = new FileStream(filePath, FileMode.Open))
向我抛出一个错误:

System.IO.IOException:进程无法访问该文件,因为它 正在被另一个进程使用

我尝试了
webClient.DownloadFile()
,但没有发生事件,但它抛出了相同的错误。如果我不处理也会抛出同样的错误。我能做什么

临时解决方法可能是
Sleep()
方法,但不是防弹的

多谢各位

编辑: 我尝试了标准处理的第二种方法,但代码中有错误

  using (WebClient webClient = new WebClient())
                {
                    // nastaveni ze webClient ma pouzit Windows Authentication
                    webClient.UseDefaultCredentials = true;
                    // <--- I HAVE CONVERT ASYNC ERROR IN THIS LINE
                    webClient.DownloadFileCompleted += new DownloadDataCompletedEventHandler(HandleDownloadDataCompleted);

                    // spusteni stahovani
                    webClient.DownloadFile(new Uri("http://czprga2001/Logio_ZelenyKyblik/Export.ashx"), TempDirectory + PSFileName);
                }

public delegate void DownloadDataCompletedEventHandler(string fileName);
        public event DownloadDataCompletedEventHandler DownloadDataCompleted;
        static void HandleDownloadDataCompleted(string fileName)
        { 
            ExcelToCsv(fileName);
        }

而且它似乎永远无法访问:/I我不明白。

尝试使用
DownloadFile
而不是
DownloadFileAsync
,就像您在编辑1中所做的那样:

string filename=Path.Combine(TempDirectory, PSFileName);
using (WebClient webClient = new WebClient())
                {
                    // nastaveni ze webClient ma pouzit Windows Authentication
                    webClient.UseDefaultCredentials = true;    
                    // spusteni stahovani
                    webClient.DownloadFile(new Uri("http://czprga2001/Logio_ZelenyKyblik/Export.ashx"), filename);
                }
ExcelToCsv(filename);  //No need to create the event handler if it is not async
从您的示例来看,似乎不需要异步下载,所以请使用同步下载并避免可能出现的相关问题,如

还可以使用
Path.Combine
组合文件夹和文件名等部分路径

它也有可能被其他东西锁定,请使用Sysinternals进程资源管理器的Find DLL或Handle函数来检查它


使用本地磁盘存储下载的文件,以防止网络出现问题。

您可能锁定了之前代码运行失败后试图创建的文件。另外,请确保您没有在任何其他应用程序中打开您试图创建/覆盖的文件。Cory的可能副本:我在下载之前删除下载的文件。Prix:我不需要检查文件是否可以访问,我需要等到下载完成。但是我会尝试一下这个功能,但它不是优雅的方式。谢谢你的回复,但它会给我带来与使用文件相同的错误。是否有可能在多个线程或类似的情况下下载两次?此外,Sysinternals Process Explorer有一个查找句柄函数,显示哪个进程真正锁定了文件。我发现问题与下载目录是网络磁盘这一事实有关,在本地磁盘上,它就像一个符咒,谢谢:-)
string filename=Path.Combine(TempDirectory, PSFileName);
using (WebClient webClient = new WebClient())
                {
                    // nastaveni ze webClient ma pouzit Windows Authentication
                    webClient.UseDefaultCredentials = true;    
                    // spusteni stahovani
                    webClient.DownloadFile(new Uri("http://czprga2001/Logio_ZelenyKyblik/Export.ashx"), filename);
                }
ExcelToCsv(filename);  //No need to create the event handler if it is not async