When I open a file, I want to know if it is being used by another process so I can perform special handling; any other IOException I will bubble up. An IOException's Message property contains "The process cannot access the file 'foo' because it is being used by another process.", but this is unsuitable for programmatic detection. What is the safest, most robust way to detect a file being used by ano开发者_JS百科ther process?
This particular version of IOException
is thrown when the error code returned from the Win32 native function is ERROR_SHARING_VIOLATION
(Documentation). It has the numeric value of 0x20
but is actually stored as 0x80070020
on the HRESULT
property of the exception (it's the result of calling MakeHRFromErrorCode).
So the programatic way of checking for a sharing violation is checking the HResult
property on the IOException
for the value 0x80070020
.
public static bool IsSharingViolation(this IOException ex) {
return 0x80070020 == Marshal.GetHRForException(ex);
}
However I do question what exactly you want to do in the scenario that it was thrown as the result of a sharing violation. The moment the exception is thrown the other process could exit and hence remove the violation.
I don't have enough "rep" to comment so hopefully this "answer" is OK...
The accepted answer is exactly what I was looking for and works perfectly, but folks here and on similar questions have questioned the utility of checking if a file is locked. It's correct that a utility function to test if a file is locked isn't much use because on the very next statement the status could have changed.
But the pattern of attempting a lock operation and then responding differently to lock errors vs. general errors is valid and useful. The most obvious thing to do is to wait for a little bit and retry the operation. This can be generalized into a helper function like this:
protected static void RetryLock(Action work) {
// Retry LOCK_MAX_RETRIES times if file is locked by another process
for (int i = 1; i <= LOCK_MAX_RETRIES; i++) {
try {
work();
return;
} catch (IOException ex) {
if (i == LOCK_MAX_RETRIES || (uint) ex.HResult != 0x80070020) {
throw;
} else {
// Min should be long enough to generally allow other process to finish
// while max should be short enough such that RETRIES * MAX isn't intolerable
Misc.SleepRandom(LOCK_MIN_SLEEP_MS, LOCK_MAX_SLEEP_MS);
}
}
}
} // RetryLock
...which can then be used like this:
public string DoSomething() {
string strReturn = null;
string strPath = @"C:\Some\File\Path.txt";
// Do some initial work...
//----------------------------------------------------------------------------------------------------
// NESTED FUNCTION to do main logic, RetryLock will retry up to N times on lock failures
Action doWork = delegate {
using (FileStream objFile = File.Open(strPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) {
// Does work here if lock succeeded, else File.Open will throw ex
strReturn = new StreamReader(objFile).ReadLine();
}
}; // delegate doWork
//----------------------------------------------------------------------------------------------------
RetryLock(doWork); // Throws original ex if non-locking related or tried max times
return strReturn;
}
...anyway, just posting in case someone with similar needs finds the pattern useful.
精彩评论