Since File Is Locked
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.
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
.
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 KarlsenWhen I faced with a similar problem, I finished with the following code:
Soul_MasterThe 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
Here is another discussion with sample code on how to use the Restart Manager API.
Markus SafarYou 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 SafarInstead 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
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 SaffronA variation of DixonD's excellent answer (above).
Usage:
Markus SafarHere's a variation of DixonD's code that adds number of seconds to wait for file to unlock, and try again:
Markus SafarThen 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 KuklauYou can see if the file is locked by trying to read or lock it yourself first.
Please see my answer here for more information.