Getting a path of a running process by name

前端 未结 3 1201
花落未央
花落未央 2021-01-12 04:22

How can I get a path of a running process by name? For example, I know there is a process named \"notepad\" running, and I want to get the path of it. How to get the path wi

3条回答
  •  长情又很酷
    2021-01-12 05:00

    There are really two approaches you can take.

    You can do process by name:

    Process result = Process.GetProcessesByName( "Notepad.exe" ).FirstOrDefault( );
    

    or you could do what you do but use linq

    Process element = ( from p in Process.GetProcesses()
                        where p.ProcessName == "Notepad.exe"
                        select p ).FirstOrDefault( );
    

提交回复
热议问题