checking if file is in use by another process

  • Thread starter Thread starter sidd
  • Start date Start date
S

sidd

hiAll,
is there a way in .net/c# to check if a file is in use with another
process.


so i am trying to do a File.Move(source,destination)..and if the file
is in use, this would through an error..System.IO.IOException..


so before i try to move the file i some how want to check if the file
is in use..if it is then i will make cuurent thread sleep for couple
of minute an then retry....

1)i wanted to accomplish this without using try catch block,if
possible
2)if it's not possible to do it without using try catch block then
also i have some question..
the exception type you get in cathc block is generic
System.IO.IOException.....this exeception could be thrown for several
reasons NOT just because the file is in use with some other
process....

other than parsing the error meassage to look for a string like 'in
use with other process' ....is there a better clean way of doing this?

thanks
siddharth
 
Hi siddharthkhare,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know whether a file is
being used by another process. If there is any misunderstanding, please
feel free to let me know.

Searching through web, I found that this can be done with WMI. Here is a
perfect example written by Willy Denoyette, for more information, please
check the the following link:

http://www.dotnet247.com/247reference/msgs/41/206104.aspx

using System;
using System.Management;
// Search the processes which have a certain file open
class App {
public static void Main() {
WhoHasThisFileOpen(@"c:\\windows\\system32\\wsock32.dll");
}
static void WhoHasThisFileOpen(string objectQry) {
SelectQuery query = new SelectQuery("select name from cim_datafile where
name ='" + objectQry +"'" );
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query)) {
foreach (ManagementObject mo in searcher.Get()) {
Console.WriteLine("File Name: {0} \nIs currently opened by:",
mo.Properties["Name"].Value);
// Get processes having this File open
foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process")) {
ShowProcessProperties(b.ToString());
b.Dispose();
}
}
}
}
static void ShowProcessProperties(string objectClass) {
using(ManagementObject process = new ManagementObject (objectClass))
{
process.Get();
PropertyDataCollection processProperties = process.Properties;
Console.WriteLine("ProcessID: {0,6} \tCommandLine: {1}" ,
processProperties["ProcessID"].Value,
processProperties["CommandLine"].Value);
}
}
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi siddharthkhare,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top