Click here to Skip to main content
13,258,479 members (53,813 online)
Rate this:
Please Sign up or sign in to vote.
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:

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:

                reader2 = new XmlTextReader(xmlUrl2);

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 16-Nov-12 5:57am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The error means what it says, in plain English. your code that wrote the file, did not close it, so it is locked.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100