Since File Is Locked

Since File Is Locked Rating: 8,9/10 2048 votes

Hotadd is a transport type that can be used when the backup host is itself a virtual machine that resides on a ESX/ESXi host that has access to all datastores via a SAN. During a virtual machine backup using the hotadd transport type the client virtual machine's disks (VMDK) files are mounted (hotadded) on the backup host as additional disks. The file might be locked because: The file is shared and another user is currently editing it. An instance of the Office app is running in the background with the file already opened. The file has been marked as Final and can no longer be updated. In some situations, you can use your mobile device to unlock the file and continue editing it.

File

This question already has an answer here:

  • Is there a way to check if a file is in use? 17 answers

Is there any way to check whether a file is locked without using a try/catch block?

Right now, the only way I know of is to just open the file and catch any System.IO.IOException.

Hossein Narimani Rad
21.9k12 gold badges68 silver badges98 bronze badges
ricreericree
12.2k13 gold badges30 silver badges27 bronze badges

marked as duplicate by Rob c#May 9 '16 at 5:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

12 Answers

No, unfortunately, and if you think about it, that information would be worthless anyway since the file could become locked the very next second (read: short timespan).

Why specifically do you need to know if the file is locked anyway? Knowing that might give us some other way of giving you good advice.

If your code would look like this:

Then between the two lines, another process could easily lock the file, giving you the same problem you were trying to avoid to begin with: exceptions.

Lasse Vågsæther KarlsenLasse Vågsæther Karlsen
300k86 gold badges536 silver badges729 bronze badges

When I faced with a similar problem, I finished with the following code:

FileSoul_Master
5,0846 gold badges50 silver badges86 bronze badges
DixonDDixonD
4,7593 gold badges24 silver badges46 bronze badges

The other answers rely on old information. This one provides a better solution.

Long ago it was impossible to reliably get the list of processes locking a file because Windows simply did not track that information. To support the Restart Manager API, that information is now tracked. The Restart Manager API is available beginning with Windows Vista and Windows Server 2008 (Restart Manager: Run-time Requirements).

I put together code that takes the path of a file and returns a List<Process> of all processes that are locking that file.

UPDATE

Shared file is locked

Here is another discussion with sample code on how to use the Restart Manager API.

Markus Safar
4,9204 gold badges20 silver badges38 bronze badges
Eric J.Eric J.
122k48 gold badges277 silver badges499 bronze badges

You can also check if any process is using this file and show a list of programs you must close to continue like an installer does.

Markus Safar
4,9204 gold badges20 silver badges38 bronze badges
AralmoAralmo

Instead of using interop you can use the .NET FileStream class methods Lock and Unlock:

FileStream.Lockhttp://msdn.microsoft.com/en-us/library/system.io.filestream.lock.aspx

FileStream.Unlockhttp://msdn.microsoft.com/en-us/library/system.io.filestream.unlock.aspx

Sergio VicenteSergio Vicente

You could call LockFile via interop on the region of file you are interested in. This will not throw an exception, if it succeeds you will have a lock on that portion of the file (which is held by your process), that lock will be held until you call UnlockFile or your process dies.

Sam SaffronSam Saffron
91.5k69 gold badges288 silver badges482 bronze badges

A variation of DixonD's excellent answer (above).

Usage:

Markus Safar
4,9204 gold badges20 silver badges38 bronze badges
TristanTristan

Here's a variation of DixonD's code that adds number of seconds to wait for file to unlock, and try again:

Markus Safar
4,9204 gold badges20 silver badges38 bronze badges
live-lovelive-love
19.8k10 gold badges92 silver badges90 bronze badges

Then between the two lines, another process could easily lock the file, giving you the same problem you were trying to avoid to begin with: exceptions.

Since File Is Locked Iphone

However, this way, you would know that the problem is temporary, and to retry later. (E.g., you could write a thread that, if encountering a lock while trying to write, keeps retrying until the lock is gone.)

The IOException, on the other hand, is not by itself specific enough that locking is the cause of the IO failure. There could be reasons that aren't temporary.

Sören KuklauSören Kuklau
15.4k5 gold badges38 silver badges72 bronze badges

You can see if the file is locked by trying to read or lock it yourself first.

Please see my answer here for more information.

Community
Brian R. Bondy

Since File Is Locked Windows 10

Brian R. Bondy
261k102 gold badges548 silver badges595 bronze badges
Markus Safar
4,9204 gold badges20 silver badges38 bronze badges
thom schumacherthom schumacher
Markus Safar
4,9204 gold badges20 silver badges38 bronze badges
Bart CalixtoBart Calixto
13.1k8 gold badges64 silver badges104 bronze badges

View Locked Files

Not the answer you're looking for? Browse other questions tagged c#.netiofilelock or ask your own question.