473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

File in Use

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

Nov 16 '05 #1
5 6716
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]
- mv*@spam.guard.caspershouse.com

"Saradhi" <up*******@inooga.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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
Nov 16 '05 #2
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ek**************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Saradhi" <up*******@inooga.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 16 '05 #3
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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:ek**************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Saradhi" <up*******@inooga.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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
Nov 16 '05 #4

"Saradhi" <up*******@inooga.com> wrote in message news:uo**************@TK2MSFTNGP15.phx.gbl...
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.

Nov 16 '05 #5
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
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...

"Saradhi" <up*******@inooga.com> wrote in message news:uo**************@TK2MSFTNGP15.phx.gbl...
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.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.