Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i need a help in a code the code is use to get a ID Of a Running Process

Like Taskmgr for example i tried to use GetProcessByName GetProcessByID and the to functions are useless not useful Please Help me i want to now the Pid of a Running Process using C# .net 


it returns
 
Process: System.Linq.Enumerable+WhereArrayIterator`1[System.Diagnostics.Process]
 ID: System.Linq.Enumerable+WhereArrayIterator`1[System.Diagnostics.Process]


any Help !? Thanks ..

What I have tried:

<pre>
            Process[] processlist = Process.GetProcesses();

            var pid2 = from theprocess in processlist where theprocess.ProcessName = "taskmgr" select theprocess ;
Posted
Updated 5-Sep-19 4:55am
v2

See: Process.GetProcessesByName Method (System.Diagnostics) | Microsoft Docs[^]
In the an array of returned Process components you can get the .Id property, see: Process Class (System.Diagnostics) | Microsoft Docs[^]

If you don't know the right name, try:
using System.Diagnostics;
...
Process[] processlist = Process.GetProcesses();

foreach (Process p in processlist)
{
    Console.WriteLine("Process: {0} ID: {1}", p.ProcessName, p.Id);
}
 
Share this answer
 
v2
Comments
MohammedZr 5-Sep-19 10:52am    
help me please i need to now the pid of a running Process
Dave Kreskowiak 5-Sep-19 16:17pm    
He has been trying to help you but he, nor I, am not going to write the code for you.

You're not understanding that you're getting an array of Process objects back from GetProcesses.

The problem you have is not with retrieving the processes. It's that you apparently don't know how to work with arrays.
There's several things wrong with this. First, you should be iterating over the pid2 variable not over processlist

Second of all, you're trying to write pid2 to the console, but this is an enumerable set. You need to print out information on the process itself (look at the Process[^] page in the documentation).

C#
foreach (Process process in pid2)
{
  Console.WriteLine("Process: {0} ID: {1}", process.ProcessName, process.Id);
  Console.ReadLine();
}

Remember, you can rely on intellisense in Visual Studio to help you out. And remember, if you're getting results that you're not expecting, use the debugger to step through your code and work out what's wrong.
 
Share this answer
 
Comments
MohammedZr 5-Sep-19 9:17am    
ok i get it Thanks i appreciate it but the problem is i need to get a pid of process is running by the process name can you help get throw it and thanks agin
MohammedZr 5-Sep-19 10:52am    
any help man please?
Chris Copeland 5-Sep-19 11:16am    
As RickZeeland explained below, you can use the Id property of a Process object to retrieve the pid.
MohammedZr 5-Sep-19 11:47am    
like this i doesn't work
var Pr = process.Id(Process.GetProcessesByName("taskmgr"));
i nee to retireve a pid from a process name you get it can you explain form please
i'm new to c#
like that

var ProcessID = from Processes in processlist where process.ProcessName=("mgr") select Processes;
Dave Kreskowiak 5-Sep-19 16:15pm    
It doesn't work because you're assuming that GetProcessesByName is returning a single Process object. It's not. It will always return an array of Process objects. You're trying to get the PID from the array, not from the Process objects in the array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900