FAQ: How do I check whether a file is in use?
-
Tuesday, April 14, 2009 10:00 AM
How do I check whether a file is in use?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
All Replies
-
Tuesday, April 14, 2009 10:03 AM
The common managed way to check whether a file is in use is to open the file in a try block. If the file is in use, it will throw an IOException.
public bool IsFileLocked(string filename) { bool Locked = false; try { FileStream fs = File.Open(filename, FileMode. OpenOrCreate, FileAccess.ReadWrite, FileShare.None); fs.Close(); } catch (IOException ex) { Locked = true; } return Locked; }
Another way to check whether a file is in use is to call the CreateFile API. If a file is in use, the handle return is invalid.
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern SafeFileHandle CreateFile(string lpFileName, FileSystemRights dwDesiredAccess, FileShare dwShareMode, IntPtr securityAttrs, FileMode dwCreationDisposition, FileOptions dwFlagsAndAttributes, IntPtr hTemplateFile); const int ERROR_SHARING_VIOLATION = 32; private bool IsFileInUse(string fileName) { bool inUse = false; SafeFileHandle fileHandle = CreateFile(fileName, FileSystemRights.Modify, FileShare.Write, IntPtr.Zero, FileMode.OpenOrCreate, FileOptions.None, IntPtr.Zero); if (fileHandle.IsInvalid) { if (Marshal.GetLastWin32Error() == ERROR_SHARING_VIOLATION) { inUse = true; } } fileHandle.Close(); return inUse; }
Related threads:
The process cannot access the file because it is being used by another process
http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/e99a7cea-43d3-49b1-82bc-5669e0b9d052
How to check if a .txt file is already open
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/76d63016-3864-4020-849a-01a82276493d/
How to check file is open or not
http://social.msdn.microsoft.com/forums/fr-FR/netfxbcl/thread/a539cbdc-5f42-4f09-9e04-860845aa049d/
How to know when a file is locked by another process?
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/4e3a6014-4cd7-4d38-ba87-ccf9ce28b3c5/
File Locked Or In Use
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/f225e48c-0321-49a3-9134-53f409dee5d9/
For more FAQ about Base Class Library, please see Base Class Library FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer by Xiaoyun Li – MSFT Tuesday, April 14, 2009 10:04 AM
-
Wednesday, January 27, 2010 8:29 PMThis is just an FYI...
The isFileLocked function didn't work in my case. It did not operate the way I expected. For example, if I opened the file from Windows Explorer and ran the code, it reported the file WAS NOT in use - although it was open. If I set a breakpoint on fs.close and open the file from Windows Explorer, I would receive the following error:
"The process cannot access the file because it is being used by another process."
Of course, I expected to receive an IOException when the file.open method was called while it was open in Windows Explorer.
Orlanzo -
Friday, January 06, 2012 9:19 AM
Look at below url :-
http://dotnet-magic.blogspot.com/2012/01/how-to-check-for-file-lock-file-in-use.html
I have used same in my Application. It is wokring.

