Solved

Protecting file / multi read and write C# / VB.Net

Posted on 2013-01-09
5
433 Views
Last Modified: 2013-01-26
Hi
Here's my scenario.

1) there is a small file in app_data say "MyFile.xml" for use in users session
2) this file is read and contents filtered/put into dictionary in each users session (to avoid multi-read for such small data) when they visit page Startup.aspx

3) this file can be changed by any user on a different page, say Config.aspx

I guess I need to use threads to prevent Read/Write conflicts between users reading file when visiting Startup.aspx and a user writing on Config.aspx, and 2 or more users trying to change/read file at same time?

if so how - is reasoning right?

Thanks
0
Comment
Question by:rwallacej
5 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38758164
Why don't you put the data in a sql table ? It will solve easily all kind of conflicts.

For using xml file in read only mode there are no issues.
But, as you correctly said, if 2 users want to modifify the data you have to use file lock mechanism, to prevent concurrent write, which is sometimes faulty on NTFS.
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 300 total points
ID: 38758374
A file lock can achieved using a mutex. This allows you lock the file across the whole system.

With a Mutex class, you call the WaitOne method to lock and ReleaseMutex to unlock. Closing or disposing a Mutex automatically releases it. Just as with the lock statement, a Mutex can be released only from the same thread that obtained it.
http://www.albahari.com/threading/part2.aspx#_Mutex

A rough example would be
mutex.WaitOne();
MyCommonMethod();
mutex.ReleaseMutex();

Open in new window


Note: If you have lots of users that require access to the file then this will be a major bottleneck and slow down the system. In this case a database will be the better method

Michael
0
 

Author Comment

by:rwallacej
ID: 38758526
Thanks.

Database is not option unfortunately - I did wonder about using this but the (small) data amount has to be easily shared amongst different PCs not necessarily running database via e.g. email and XML file was used as option
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 200 total points
ID: 38759429
If you use a FileStream to read and write the file, you can specify the FileShare value so that file can still be read while its being written.

http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx

http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
0
 

Author Closing Comment

by:rwallacej
ID: 38821992
great thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now