tags:

views:

3924

answers:

9

This is how I do it at the moment. I try to open the file with the FileShare set to none. So I want exclusive accesss to the file. If I can't get that then its a good bet somebody else has the file locked.

There's got to be a better and faster way. Any ideas?

            try
            {
                using (FileStream fs = File.Open(GetLockFilename(), FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                {
                    fs.Close();
                }
                // The file is not locked
            }
            catch (Exception)
            {
                // The file is locked
            }
+11  A: 

There is no need first to check if the file is locked and then access it, as between the check and the access some other process may still get a lock on the file. So, what you do is correct, if you succeed, do your work with the file.

Sunny
Also, you can tell from the type of exception thrown if the file is locked or if it's a permissions issue.
Binary Worrier
Exactly. This is the same fallacy that people fall for when first trying to check if a file exists and then trying to open, delete or whatever it.
Christian.K
I think the exception is FileAccessException...
trace
+1  A: 

No that I am aware of, there is no call to check if the file is in use - you have to try to open it and handle the exception as you are doing. Another problem is that it is hard to distinguish between in use and no access allowed.

Otávio Décio
+3  A: 

The truth is, even if you do figure out a way to check if the file is "locked" by the time you get to the next line where you open the file, something else in the OS may try to get a hold of that file, and your code to open it will fail anyway. You'll have to put a try/catch there anyway. Therefore, I say no. There isn't really a better solution.

BFree
+2  A: 

There was a similar question here that might help:

http://stackoverflow.com/questions/1304/how-to-check-for-file-lock-in-c

Kev
A: 

Thanks for the answers. What I'm trying to do is detect the condition of a lock file being orphaned by a process preventing further updating of the database. Whilst this is a boundary condition I thought it'd be nice to write a unit test for it. Unfortunately, I've now broken the usual case unit test in the process of trying to detect for an orphaned lock file. Maybe I shouldn't worry so much...

Jack Hughes
A: 

I have written a lot of information on this topic.

Please see my answer here for more information.

Brian R. Bondy
A: 

What Sunny said, but remember that you should specify what Exception it is you catch.

Svish
A: 

Look at my answer for another similar question.

DixonD
A: 

I am surprised that everyone assumes that checking is useless. I have a common scenario where files are delivered by an FTP process. My ETL process then needs to load the data from the file into a database. I need to check if the FTP process has finished writing to the file. I have no control over the FTP process so cannot request them to rename the file after writing is completed. So I need a function to check if the file is locked or not. I cannot do it on file by file basis. I actually need to load files in sequence, so I need to go through the lot and make decisions based on which ones are locked. In summary, a function to check locked status of the file would be very useful. Workaround that is suggested of trying to lock or open file and trip an error will work, but is a bit uintrusive and creates unnecessary IO overhead. I would much rather get the info from the OS without making any changes to the file...

DejanN