You could start by taking a look at Handle By Mark Russinovich. Just run it as administrator and it will return all files used by all processes.
You could use the following syntax to put the results into a text file:
handle.exe > log.txt
Afterwards, you may use PowerShell
to extract the information about the processes using those data files:
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()}
}
Here’s the same approach from C# (you need to run the application as Administrator):
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;
}
}
13
solved get current process name with file extension in c# [closed]