Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I am trying to download a file for xmlreader purposes. The file can download fine, but it won't let me read it.

This is what I have so far:

C#
public void downloadXmlFile()
        {
            WebClient webClient = new WebClient();
            //webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadFileAsync(new Uri("http://mysite", "c:\newxmlfile.xml");
        }


This is how I try to read it:

C#
try
            {
                reader2 = new XmlTextReader(xmlUrl2);
                reader2.MoveToContent();
            }


It's not liking the new XmlTextReader(xmlUrl2) which is the string of "C:\newxmlfile.xml".

Am I missing something obvious? The error message I'm getting is:
The process cannot access the file 'C:\newxmlfile.xml' because it is being used by another process.
Posted

The error means what it says, in plain English. your code that wrote the file, did not close it, so it is locked.
 
Share this answer
 
DownloadFileAsync returns as soon as it's started, instead of waiting until the download is complete.

Therefore it may still be downloading the file when you try to read it, which would mean the file is used by that process.

Either use DownloadFile or wait for the task to finish.
 
Share this answer
 
Comments
joshrduncan2012 16-Nov-12 11:33am    
Thanks Orjan! That worked, now the XML file is downloading alot of other garbage along with what is actually contained in the file, such as headers and other stuff.
Orjan Westin 16-Nov-12 11:56am    
In that case, can you please mark the question answered? :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900