handle.exe を使用して、どのプロセスがファイルを所有しているかを調べようとしています。コマンドラインから独自にハンドルを実行すると、期待どおりに動作します。ただし、コード内から実行すると、ファイルをロックしているプロセスがないことが常に返されます。
Handle.exe を実行する私のコード:
Process tool = new Process();
tool.StartInfo.FileName = "handle.exe";
tool.StartInfo.Arguments = theFileName;
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();
string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach (System.Text.RegularExpressions.Match match in
System.Text.RegularExpressions.Regex.Matches(outputTool, matchPattern)) {
Process p = Process.GetProcessById(int.Parse(match.Value));
Console.WriteLine("Holding Process: " + p.Id);
}
また、この SO の質問で提案されているファイルの所有権を見つけるための他の方法をいくつか試しまし た。 ファイルを制御できるものは何もないとまだ報告しています。
このことをテストするために、基本的にこれを実行するだけの別のテストプログラムがあります。
using (FileStream theStream = new FileStream(theFileName, FileMode.Open, FileAccess.Read, FileShare.None)) {
while (true) ;
}
編集: Visual Studio を管理者として実行しているため、コードから開始するすべてのプロセスが同じ権限を取得します。もちろん、これはデバッグ中のみですが、他の環境を心配する前に、少なくとも 1 つの環境で動作させる必要があります。
ハンドルが実行された後、outputTool は
ハンドル v3.5 Copyright (C) 1997-2012 マーク・ルシノビッチ Sysinternals - www.sysinternals.com 一致するハンドルが見つかりません。