Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current ProcessID?

Tags:

.net

process

What's the simplest way to obtain the current process ID from within your own application, using the .NET Framework?

like image 404
plaureano Avatar asked Jun 09 '10 07:06

plaureano


People also ask

How do I find out what PID I am running on Linux?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

How do I find the process ID in Windows CMD?

Use the Command PromptIn the Start menu search bar, search for command prompt and select Run as administrator. Type tasklist. Press Enter. Command Prompt will now display the PID for the running processes.


2 Answers

Get a reference to the current process and use System.Diagnostics's Process.Id property:

int nProcessID = Process.GetCurrentProcess().Id; 
like image 136
luvieere Avatar answered Sep 24 '22 21:09

luvieere


Process.GetCurrentProcess().Id 

Or, since the Process class is IDisposable, and the Process ID isn't going to change while your application's running, you could have a helper class with a static property:

public static int ProcessId {     get      {         if (_processId == null)         {             using(var thisProcess = System.Diagnostics.Process.GetCurrentProcess())             {                 _processId = thisProcess.Id;             }         }         return _processId.Value;     } } private static int? _processId; 
like image 43
Joe Avatar answered Sep 25 '22 21:09

Joe