Handle By Mark Russinovichを見ることから始めることができます。管理者として実行するだけで、すべてのプロセスで使用されるすべてのファイルが返されます。
次の構文を使用して、結果をテキスト ファイルに入れることができます。
handle.exe > log.txt
その後、PowerShell
これらのデータ ファイルを使用してプロセスに関する情報を抽出するために使用できます。
Get-Content log.txt |
where{$_.readcount -gt 6} |
foreach{
if($_.Substring(0,1) -ne " " -and $_.Substring(0,1) -ne "-")
{$process = $_.ToString()}
elseif($_.ToLower() -like "*.avi" `
-or $_.ToLower() -like "*.mkv" `
-or $_.ToLower() -like "*.mpg" `
-or $_.ToLower() -like "*.mp4" `
-or $_.ToLower() -like "*.wmv" `
)
{$process.ToString()}
}
C# からの同じアプローチを次に示します (アプリケーションを管理者として実行する必要があります)。
class Program
{
static void Main(string[] args)
{
var processes = GetProcesses();
// enumerate the processes
foreach (Tuple<int,string> mediaFile in processes.Distinct())
{
var process = Process.GetProcesses().Where(i => i.Id == mediaFile.Item1).FirstOrDefault();
Console.WriteLine("{0} ({1}) uses {2}", process.ProcessName, process.Id, mediaFile.Item2);
}
Console.ReadLine();
}
private static List<Tuple<int,string>> GetProcesses()
{
string line = "";
int counter = 0;
string currentProcess = "";
List<Tuple<int, string>> mediaFiles = new List<Tuple<int, string>>();
Process compiler = new Process();
compiler.StartInfo.FileName = @"c:\YourPath\Handle.exe";
compiler.StartInfo.CreateNoWindow = true;
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();
while ((line = compiler.StandardOutput.ReadLine()) != null)
{
// skipping applicaion info
if (++counter > 6)
{
if (!" -".Contains(char.Parse(line.Substring(0, 1))))
{
currentProcess = line;
}
else if ((new[] { ".avi", ".mkv", ".mpg", ".mp4", ".wmv" })
.Contains(line.ToLower().Substring(line.Length - 4)))
{
int pos = currentProcess.IndexOf("pid:") + 5;
string pid = currentProcess.Substring(pos, currentProcess.IndexOf(" ", pos) - pos);
mediaFiles.Add(new Tuple<int, string>(Int32.Parse(pid),line.Substring(21)));
}
}
}
compiler.WaitForExit();
return mediaFiles;
}
}