How to find if file is already open.
-
Monday, April 30, 2012 4:36 PM
Hi,
I'am storing word documents content in DB. Then i'm reading the contents of the file and placing it in isolated store for application and user.using System.Diagnotics. Process.Start(physicalfilepath) i'm trying to start the process. The document opens. My requirement is if the document is already open i need to show a message to user that file is already open. How to achieve this functionality. Kindly advice.
Regards,
Megarekaa.
All Replies
-
Monday, April 30, 2012 5:04 PM
A dirty way is to try and open it:
try
{
using (FileStream fs = new FileStream("filename.TXT",FileMode.Open))
{
}
}
catch (IOException e)
{
MessageBox.Show("File is allready open!");
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Wednesday, May 09, 2012 2:40 AM
-
Monday, April 30, 2012 8:21 PM
You can use the WMI class CIM_LogicalFile and check the InUseCount. It gives the number of "file opens" that are currently active against the file.My blog
Whether you’re a construction worker, a forum moderator, or just someone that likes helping people. I think these guidelines can be helpful in keeping you helpful when being helpful.- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Wednesday, May 09, 2012 2:40 AM
-
Monday, April 30, 2012 8:25 PM
Or alternatively:
protected virtual bool IsFileLocked(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { //the file is unavailable because it is: //still being written to //or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; }
John Grove, Senior Software Engineer http://www.digitizedschematic.com/
- Proposed As Answer by JohnGrove Wednesday, May 02, 2012 2:17 PM
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Wednesday, May 09, 2012 2:40 AM
-
Monday, April 30, 2012 9:02 PM
I don't know how Mark Russinovich from SysInternals did it, but you can use this handle.exe as an external tool to find out: http://technet.microsoft.com/en-us/sysinternals/bb896655
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Wednesday, May 09, 2012 2:40 AM

