Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
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 16-Nov-12 4:57am
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.
  Permalink  
Comments
joshrduncan2012 at 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 at 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
0 OriginalGriff 349
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Richard Deeming 135
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2014
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