-3

GetAllProccess Function Return All Runing Proccesses In Windows. I want to get current proccess name which extension is ".avi", ".mkv", ".mpg", ".mp4", ".wmv"
e.g. if I play any video file in windows media player it return (wmplayer.exe) or if I play any video file in KM PLAYER it returns(kmplayer.exe)
Thanks here is my code this code working very slow reference http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318

string filename; Process[] procs = Process.GetProcesses() ; foreach (Process prc in procs) {

            if (procs.Length > 0)
            {
                int id = prc.Id;
                IEnumerator<FileSystemInfo> fie = DetectOpenFiles.GetOpenFilesEnumerator(id);

                while (fie.MoveNext())
                {
                    if (fie.Current.Extension.ToLower(CultureInfo.InvariantCulture) == ".mp3")
                    {
                        filename = fie.Current.FullName;
                        break; // TODO: might not be correct. Was : Exit While
                    }
                }
            }
        }
user757321
  • 321
  • 8
  • 17

2 Answers2

4

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;
    }
}
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
1

Everything you need to do is enumerate all processes and find the process to which your handle belongs to. ..NET doesn't provide the AFI for this, you need to dive deeper, more deep then WINAPI - to the nt.dll level, where you can find undocumented ZwQueryObject().

Using this methods is not a simple task, because it returns name and info about handle belonging to the YOUR process. So, you need to perform additional task - use DuplicateHandle() to bring external handle to your process.

I recomend you to study this sample http://www.codeguru.com/Cpp/W-P/syst...icle.php/c2827

which provide all functional u requre.

fibertech
  • 357
  • 1
  • 13