File level locking Properties.Settings

129 Views Asked by At

I am pretty new to C# and wanted to save some settings. My application has to be able to run multiple instances therefore it is a possibility of several simultaneous writes to the user.config file. Causing the config file to become corrupted. Therefore I wonder if there are any File level locking when they are saved.

Tried to take a look on the source code for .Save and this seems to be boiling down to the interface call IInternalConfigHost.OpenStreamForWriting. But it is an interface so I can not easily answer this question on my own.

Updated the wording somewhat.

Basically I am wondering if the user.config file can become corrupt due to several simultaneous writes to it from several instances of my application.

2

There are 2 best solutions below

1
afewcc On BEST ANSWER

If two or more instances of your program try to write to the same configuration file, you will get exceptions while calling .Save

What you can do is use the Mutex Class to synchronize access from multiple instances. You can check this answer on how to do it properly.

However, one thing that you need to think about is : if one of your instances updates the configuration file, should the others be notified and reload it to provide a consistent behaviour?

0
Kim Ryunghi On

You can use mutex lock object. It is prevents simulataneouse access to shared object bewtween multi thread and multi process.

Reference following page. http://derpturkey.com/c-cross-process-synchronization/