Get current process name with file extension in C #

The GetAllProccess function returns all running processes in Windows. I want to get the current proccess name, the extension of which ".avi", ".mkv", ".mpg", ".mp4", ".wmv"


for example if I play any video file in Windows Media Player it returns (wmplayer.exe), or if I play any video file in KM PLAYER it returns (kmplayer.exe)
thanks to here is my code. this code is very slow link http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318

string filename; Process [] procs = Process.GetProcesses (); foreach (prc process 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
                    }
                }
            }
        }

      

-3


source to share


2 answers


You can get started by looking at "Handle By Mark Russinovich" . Just run it as administrator and it will return all files used by all processes.

You can use the following syntax to put the results in a text file:

handle.exe > log.txt

      

Then you can use PowerShell

to retrieve information about processes using these 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 app 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;
    }
}

      

+3


source


All you have to do is list all the processes and find the process that your pen belongs to. ..NET does not provide AFI for this, you need to dive deeper, deeper then WINAPI - to the nt.dll level where you can find the undocumented ZwQueryObject ().

Using these methods is tricky because it returns the name and information about the handle that belongs to YOUR process. So, you need to do an extra task - use DuplicateHandle () to make an external handle for your process.



I recommend you study this sample http://www.codeguru.com/Cpp/WP/syst...icle.php/c2827

which provide all functional requirements.

+1


source







All Articles