lock an xmlfile

S

Shawn

Hi.
I have a webservice method that receives an XmlNode. It then loads an
existing xml file into an XmlDocument, appends the new XmlNode to the
XmlDocument and saves it back to the xml file. But what if another user is
doing the same thing at the same time? I did a test and found out that the
file is not automatically locked. Which means that the second user
overwrites the changes the first user made. Any ideas on how I can prevent
this?

Thanks,
Shawn
 
P

Peter Rilling

What happens is that the XML content is loaded entirely in memory and the
DOM is created. So normally there are no connections to the file, unlike
when you have a stream.

Have not tried this but what you might try doing is to manually open the
file using the FileStream class (you can specify locks using the FileShare
enumeration), then use that stream when you call the XmlDocument.Load.
 
S

Shawn

Thanks Peter, I think that will do the trick. One thing though. If the
file is closed by another FileStream, is there a way I can wait until the
file is no longer locked instead of just writing an error message to the
user? Something like this:
Do While file.isClosed
'Just waiting for the file to open
Loop

Thanks,
Shawn


What happens is that the XML content is loaded entirely in memory and the
DOM is created. So normally there are no connections to the file, unlike
when you have a stream.

Have not tried this but what you might try doing is to manually open the
file using the FileStream class (you can specify locks using the FileShare
enumeration), then use that stream when you call the XmlDocument.Load.
 
P

Peter Rilling

Now you get into more interesting architectural issues.

The simplest way might be a bruit force method in which you use the loop but
use a try...catch block so the exception does not bubble (I do not know how
to ask the OS if a file is locked).

Another way might be to look into using the Mutex class. You could wrap
access to the file so that a Mutex is always created when the file is
opened, then released when closed.
 
W

William LaMartin

The problem I ran into when using Mutex is that in a shared hosting
environment, its use would throw a security exception. I could never really
get the hosting company to understand the problem. I am not even sure
anyone there understood the question--what needed to be set so as to allow
the use of Mutex. Mutex was not in their vocabulary.

Its use worked fine on my local setup, though. So if you have control over
the server, then I think Mutex is the way to go in restricting the use of
files while they are being updated.
 
S

Shawn

I've tried looking into Mutex and how to use it to lock a file with VB.NET,
but I haven't found any examples. Could you show me to an example?

Thanks,
Shawn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top