views:

43

answers:

1

Is it possible to use a lock on one xml file that can potentially be written to from multiple aspx pages at the same time? I'm asking because MSDN suggest that the lock statement should be used with a private static object instance as the expression, and since there are multiple pages involved i guess i can't use the same object on all the pages?

A: 

You can use the File.Open overload that takes a FileShare enum that is set to None. No other thread will be able to open the file until closed.

FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None);

This piece of code will open the (existing) file specified in the path argument, for reading and without any sharing.

Oded
Will this code throw an error on subsequent reads? or block and wait?
Wallace Breza
@Wallace Breza - I believe it will throw an exception.
Oded