File in Use

  • Thread starter Thread starter Saradhi
  • Start date Start date
S

Saradhi

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
 
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
 
Hi,

IT should be somehow, if you use Process Explorer from sysinternals.com it
lists the files open and the opener process., I don;t know if they provide
code for it though.

Cheers,
 
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
 
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.



It should work with all files, Windows has no knowledge about file extentions - a file is a file- and it's in use or it is not.

If you got a error like "query is invalid", it means the ... right, the query is invalid !

So you need to examine the query string, probably you have a special character in it like the ' in Johnston's Book.

Willy.
 
it still doesnt work for a simple valid file query like C:\\123.xls

I guess the query shouldnt be a problem.

Also while debugging , I observed that the control doesnt go in to the

foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process"))

{

string p = ShowProcessProperties(b.ToString());

b.Dispose();

x +=p;

}

loop . that is it is not able to find the WIN32 processes locking this Excel file. But I opened it using the Excel and Excel application should be locking this Excel File.

There is something fundamentally wrong in the way I am using this code.

I have tested for all types of files and this works only for exes and dlls and doent work for other types of files.


-SARADHI

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.



It should work with all files, Windows has no knowledge about file extentions - a file is a file- and it's in use or it is not.

If you got a error like "query is invalid", it means the ... right, the query is invalid !

So you need to examine the query string, probably you have a special character in it like the ' in Johnston's Book.

Willy.
 
Back
Top