简体   繁体   中英

Title name to Process name in C#

I want to find the name of a process through inputting the title of the program. For example, if I type "Google Chrome", I want the console to print "chrome" (how the process name looks in task manager) Thank you

There is no property in Process like in Task Manager Process Description, You can find the FileDescription of EXE by using FileVersionInfo

var processes = Process.GetProcesses().ToList();

foreach (var p in processes)
{
    try
    {
        var description = FileVersionInfo.GetVersionInfo(p.MainModule.FileName).FileDescription;
        if (description == "Google Chrome")
        {
            Console.WriteLine(p.ProcessName);
            break;
        }

    }
    catch (Exception ex)
    {
        // You will get Access is denied exception for some processes when accesses `MainModule`
    }
}

Note : Run your application as Administrator

The Process object has a property called MainWindowTitle which is likely the property you are referring to. The System.Diagnostics namespace is where the Process objects are located. You can then use Process.GetProcesses() to retrieve all of the processes running on your system. You then iterate through this list and look for one that has a window Title that matches the value you are looking for, break from the loop, and output the proc.ProcessName;

foreach(Process proc in Process.GetProcesses())
{ 
     if(proc.MainWindowTitle == "Google Chrome")
     { 
        Console.WriteLine(proc.ProcessName); 
        break;
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM