Dear Nicholas,
Thanks for your quick Response.
I made some research and tried to do this using WMI classes as follows:
static void WhoHasThisFileOpen(string objectQry)
{
string x = null;
SelectQuery query = new SelectQuery("select name from cim_datafile where name ='" + objectQry +"'" );
using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
{
foreach (ManagementObject mo in searcher.Get())
{
x = "File Name: " + mo.Properties["Name"].Value + "\n Is currently opened by:\n";
// Get processes having this File open
foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process"))
{
string p = ShowProcessProperties(b.ToString());
b.Dispose();
x +=p;
}
}
MessageBox.Show(x);
}
}
static string ShowProcessProperties(string objectClass)
{
using(ManagementObject process = new ManagementObject (objectClass))
{
process.Get();
PropertyDataCollection processProperties = process.Properties;
string x = null;
x = "ProcessID := " + processProperties["ProcessID"].Value + "Command Line = " + processProperties["CommandLine"].Value +"\n";
return x;
/*Console.WriteLine("ProcessID: {0,6} \tCommandLine: {1}" ,
processProperties["ProcessID"].Value,
processProperties["CommandLine"].Value);
*/
}
}
private void button1_Click(object sender, System.EventArgs e)
{
WhoHasThisFileOpen(@"C:\\windows\\winsock.dll");
WhoHasThisFileOpen(@"C:\\Documents and Settings\\upadrasta\\Local Settings\\Temp\\Palantir\\Daniel Johnston's Book Example--P50.xls");
}
The problem is here if I uses dll or exe files, this program works and displayes the processes that uses this file.
But if I uses any notepad file or excel file or other files, this doesnt work.
The problem I gues is in the statement "
SelectQuery query = new SelectQuery("select name from cim_datafile where name ='" + objectQry +"'" );
"
as I am getting exception saying that the query is invalid.
I guess thatthe cim_datafile is for exec and dlls. Is anything We can use here for data files like text files, word files and excel files.
Thanks for Your Patience.
- SARADHI
Saradhi,
There really isn't a way to do this, as it is dependent on the
application. Notepad, for example, loads the file into memory, and then
views the contents. Word and Excel, have a lock on the file.
The best you can do is try and get write access (most apps should allow
reads, but not writes if they are sharing at all) to a file. You would call
the CreateFile API through the P/Invoke layer, and look to see if an error
is returned while opening. The API will return a specific error code if the
file is not shared.
As for getting the process information, I don't know if that information
is exposed by the API,
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
How can I know whether a file in in use or it in C#??
For ex, if I have
C:\\Documents and Settings\\upadrasta\\Local
Settings\\Temp\\Palantir\\Daniel Johnston's Book Example--P50.xls
I need to know whether this file is in use or it.
if yes, I need the processID of that process who is using this File.
Anybody can help me with some source code??
-SARADHI