Using .Net Framework, it can be achieved here: How to get the current ProcessID?.
But how to get current PorcessId in UWP? System.Diagnostics does not even have Process class.
Using .Net Framework, it can be achieved here: How to get the current ProcessID?.
But how to get current PorcessId in UWP? System.Diagnostics does not even have Process class.
using Windows.System.Diagnostics;
var processId = ProcessDiagnosticInfo.GetForCurrentProcess().ProcessId;
As UWP allow win32 api GetCurrentProcessId, you can call it from c#. I used to call it from c++/winrt uwp project.
using System.Runtime.InteropServices;
class public xxx{
[DllImport("Kernel32.dll", EntryPoint = "GetCurrentProcessId")]
static extern UInt32 GetCurrentProcessId();
public void xxx(){
var processId = GetCurrentProcessId();
Debug.WriteLine("my process Id:"+ processId.ToString());
}
}
Ps1:
Windows.System.Diagnostics.ProcessDiagnosticInfo.GetForCurrentProcess()
crash sometimes in my uwp c# project(not always crash). Get exception of "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Ps2:
System.Diagnostics.Process.GetCurrentProcess()
not exist in my uwp c# project . https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.getcurrentprocess?view=netframework-4.8