How to get the current ProcessID?

后端 未结 3 1424
囚心锁ツ
囚心锁ツ 2020-12-02 22:10

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

3条回答
  •  时光取名叫无心
    2020-12-02 22:50

    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;
    

提交回复
热议问题