C# 识别托管代码中文件的进程句柄和锁

C# 识别托管代码中文件的进程句柄和锁,c#,.net,C#,.net,我正在使用DotNetZip库压缩文件夹中的文件。为了识别当前由其他进程打开的文件,我使用SysInternals.com中的“handle.exe”。我通过使用参数调用它并解析输出来实现这一点,方法如下 using (Process handleProcess = new Process()) { // -- Set up the parameters and call the process. handleProcess.StartInfo.FileName = "handl

我正在使用DotNetZip库压缩文件夹中的文件。为了识别当前由其他进程打开的文件,我使用SysInternals.com中的“handle.exe”。我通过使用参数调用它并解析输出来实现这一点,方法如下

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();
...
这是可行的,但有一种混乱的气氛。有人能在托管代码中提出更好的方法吗?

。在另一个问题中,其他人也提出了SysInternals


无论您做什么,都需要有重试逻辑。我以前写过适配器,但必须重试。重试逻辑本身被封装在一个RetryTracker类中,该类包含用于不同算法的子类,例如线性延迟、步进延迟等。

以下代码显示了由其他进程打开的文件:

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);
    }
}
这是一个稍微修改过的版本