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;
The upcoming .NET 5 introduces Environment.ProcessId which should be preferred over Process.GetCurrentProcess().Id as it avoids allocations and the need to dispose the Process object.