问题
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