0

I am zipping files in a folder using the DotNetZip libraries. To identify files that are currently open by other processes I am using 'handle.exe' from SysInternals.com. I do this by calling it with parameters and parsing the output, along these lines.

using (Process handleProcess = new Process())
{
    // -- Set up the parameters and call the process.
    handleProcess.StartInfo.FileName = "handle.exe";
    handleProcess.StartInfo.UseShellExecute = false;
    handleProcess.StartInfo.RedirectStandardOutput = true;
    handleProcess.StartInfo.Arguments = "-u " + fileName;
    handleProcess.Start();
...

Which works but has the air of a kludge about it. Can anyone suggest a better approach within managed code ?

Alan B
  • 162
  • 2

2 Answers2

1

The following code shows you the files opened by other processes:

SelectQuery query = new SelectQuery("select name from cim_datafile");
using (ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
    foreach (ManagementObject mo in searcher.Get())
    {
        Console.WriteLine("File Name: {0} \nIs currently opened", mo.Properties["Name"].Value);
    }
}

It is a slightly modified version of this.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
0

See the answers in a similar question asked on SO. In another question using interop was suggested (and someone else suggested SysInternals as well).

No matter what you do, you need to have retry logic. I've written adapters before that have had to retry. The retry logic itself was encapsulated in a RetryTracker class with sub classes for different algorithms (e.g. linear delay, stepping delay, etc.).

Community
  • 1
  • 1
Kit
  • 20,354
  • 4
  • 60
  • 103