Reputation: 67223
I have a simple app which is writing to an xml file on every button click. Once this is done, I read the Xml file soon after (as in, a couple of lines below with no methods to be stepped in to in between).
When the app runs for the first time, the xml file is written to/read from fine, but if I press the button again I get a "File is in use by another process". I am doing the whole flush, close, dispose thing with my streams and intend on using ProcessMon to check what process is holding the file.
Programatically, what is the best strategy to avoid this problem?
Thanks
Upvotes: 0
Views: 176
Reputation:
I think I answered this one already elsewhere. Call the Flush() method just before you close, and use separate FileStream objects. I think the FileStream object is the problem, which is why it is always good to Flush() when you are done writing.
There is not equivalent method for Readers, though. that is why I wound up using to FileSTream objects.
Rudedog
Upvotes: 0
Reputation: 17471
the easiest solution would be to just lock all of the code that accesses the xml file
i.e.
object locker = new Object();
....
lock (locker)
{
//write xml file
}
Upvotes: 0
Reputation: 8209
Just make sure the file is written and then use the in memory data you just wrote to the file.
You don't need to read it again.
Upvotes: 1