I want to open a file for read in exclusive mode, and if the file is already opened by some process/thread else, I want to receive an exception. I tried the following code,
What you have done is correct.
If you need what are all files already opened, then there is a way to see by NtQuerySystemInformation
You may get idea from http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx
which gets all the files opened in a directory.. which can be extended to a single file whether opened or not...