C# process cannot access file because it is being used by another process

主宰稳场 提交于 2019-12-06 12:05:40

问题


Please help me out with the problem I am facing.

I am writing an importer in C# for an xml file. Every time I run the import I need to download the XML file from a URL.

I have wirtten the following code to download it:

var xmlPath = @"C:\Desktop\xxx.xml";
public void DownloadFile(string url, string saveAs)
{
    using(var webClient = new WebClient())
    {
        webClient.DownloadFileAsync(new Uri(url), saveAs);
    }
}

and _downloader.DownloadFile(Config.FeedUrl, xmlPath); to call the method. The Url is in the config file (Config.FeedUrl).

Then when I am trying to GetProperties(xmlPath); I get the Exception "Process Cannot access the file because the file is being used by another process.

I made sure that the destination exists but i am not sure why I get this error.

Can somebody help me out?

Thanks


回答1:


Looks like your asynch download operation is yet to complete when you try to access the properties. Have you made sure that the download is completed before accessing the file?

You can access the file in the DownloadFileCompleted event.

http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadfilecompleted.aspx



来源:https://stackoverflow.com/questions/5140481/c-sharp-process-cannot-access-file-because-it-is-being-used-by-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!