开发者

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

开发者 https://www.devze.com 2023-02-13 16:40 出处:网络
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 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:\Des开发者_JAVA百科ktop\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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消