What's the simplest way to obtain the current process ID from within your own application, using the .NET Framework?
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.
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.
Get a reference to the current process and use System.Diagnostics
's Process.Id
property:
int nProcessID = Process.GetCurrentProcess().Id;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With