Connecting Tech Pros Worldwide Forums | Help | Site Map

File being used by another process

Newbie
 
Join Date: Jun 2009
Posts: 2
#1: Jun 19 '09
I'm having a very erratic behavior when I try to create a new xml file using XmlTextWriter. Sometimes it works, but most of the time it throws that exception.
It feels like the program is not letting itself access the file.

Expand|Select|Wrap|Line Numbers
  1. XmlTextWriter xt = new XmlTextWriter(filePath, null);
  2.  
filePath is c:\xmltest\test.xml

I have no idea what may be the problem, and it's very frustrating. Any kind of help will be greatly appreciated.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,777
#2: Jun 19 '09

re: File being used by another process


Is the error actually at creation, or on a following line when you try to write into the file?

What about anti-virus seeing the new file and trying to scan it?
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 702
#3: Jun 22 '09

re: File being used by another process


You need to dispose of XmlTextwriter ( in case the error is generated after you have created the file while accessing second time). Using statement would be better
Expand|Select|Wrap|Line Numbers
  1. using(XmlTextWriter xt = new XmlTextWriter(filePath, null))
  2. {
  3. }
  4.  
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Jun 24 '09

re: File being used by another process


PRR's way is great. The XmlTextWriter (and all the other stream readers and writers) implement IDisposable, which provides a means to clean up after themselves.

What is happening under the covers:
When you instantiate a new XmlTexWriter with a path as a parameter, it is creating a FileStream with a FileAccess of "Write." In windows, that puts a lock on the file for editing, basically, the process that your program is on has an exclusive lock on that file.

It maintains this lock until you call the .Close() method on your stream. If you terminate your program before you release the file, the file will remain locked. Eventually the OS will unlock the file again, but you are unable to write to the file again until that happens, and it doesn't happen quickly.

Fortunately, in it's implementation of the IDisposable interface, in the Dispose() method, this.Close() is called. So, when you do this:
Expand|Select|Wrap|Line Numbers
  1. using (XmlTextWriter xt = new XmlTextWriter(somePath, null))
  2. {
  3.  
  4. }
when that code block exits, it automatically calls the .Dispose() method on the object in the using statement. Your stream will close, the process will release the lock on the file.

Sorry for the long explanation, but I feel that it is important to understand what is going on under the covers.
Newbie
 
Join Date: Jun 2009
Posts: 2
#5: Jul 3 '09

re: File being used by another process


Thanks everyone for your replies. Sorry I couldn't get back to you before, I was on a trip. I prefer long explanations, they're just...better. Anyway, I finally made it work, thanks again!
Reply