简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM