你可以先看看Mark Russinovich 的 Handle。只需以管理员身份运行它,它将返回所有进程使用的所有文件。
您可以使用以下语法将结果放入文本文件中:
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;
}
}