tags:

views:

217

answers:

4

Hi, We have a requirement as follows for handling XML files in C# code. Please provide logic/solution in such a way to achieve this.

Problem: We have an XML file,assume it has test.xml. When a user 'A' working with test.xml then we want the requirement that to lock this XML and to process like read/write etc. In the sametime a user 'B' also tried to access the test.xml , we want the requirement that user 'B' should not able to open and 'B' has to wait until the lock of user 'A' is to be release or unlock.

Also we shouldn't get any error/exception till the user 'B' is in waiting mode and we don't want to show any message to the user . As we are not interested to use FileStreams in this scenario due to performance issue.

Can anyone help in this regard how to overcome this type of issue.

Please Note: We do not want to use filestreams. Please suggest an alternative solution in this regard.

A: 

What have you come up with so far?

Is this a client/server app, stand alone app? Are you looking to implement something like MS Excel locking, or varaible use to indicate locking in the app itself?

astander
Not XML locking. We are planning to use mutex. not use will it overcome related to performance. And also how to proceed....
sukumar
This is client application only. Duw to client requirements we are not using Filestream concept here due to performance too...
sukumar
A: 

I suppose you would have to open the file in READ/WRITE mode and make some kind of loop to check if the file is not open.

 try {
    FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Write);
   } catch(Exception e){
     // TODO check if access denied 
   }

If you prefer do those kind of test before I advise you to look in semaphores.

RageZ
We do not want to use any FileStream concept because it will slow down the performance for a large size of XML files.Regarding performance issue we are not using Filestreams.It would be extremely grateful if you could suggest an alternative solution.
sukumar
@sukumar: I have suggested using `semaphores` or `mutex`
RageZ
A: 

Why not use Mutexes in this case - you want to restrict other user's access to the file altogether if one user has opened it and working on it.

MSIL
+3  A: 

The technique I am familiar with is to create a lock file as a sibling to the file you are locking. All clients must create this lock file if they want read/write permission on the file in question, and cannot read/write the file if they did not create the lock file. When they are done reading/writing, they close access to the file and delete the lock file. Clients waiting to write, then, should poll for the existence of the lock file, and when it does not exist, they create it and use the file in question freely.

fbrereto