C# 如何在UWP中获取当前进程ID?
使用.Net Framework,可以在以下位置实现: 但是如何在UWP中获得当前进程ID?C# 如何在UWP中获取当前进程ID?,c#,uwp,C#,Uwp,使用.Net Framework,可以在以下位置实现: 但是如何在UWP中获得当前进程ID? System.Diagnostics甚至没有进程类。由于UWP允许win32 api GetCurrentProcessId,您可以从c#调用它。我过去常常从c++/winrt uwp项目中调用它 using Windows.System.Diagnostics; var processId = ProcessDiagnosticInfo.GetForCurrentProcess().ProcessId
System.Diagnostics甚至没有进程类。由于UWP允许win32 api GetCurrentProcessId,您可以从c#调用它。我过去常常从c++/winrt uwp项目中调用它
using Windows.System.Diagnostics;
var processId = ProcessDiagnosticInfo.GetForCurrentProcess().ProcessId;
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()
在我的uwp c#项目中有时会崩溃(并不总是崩溃)。获取“尝试读取或写入受保护内存”的异常。这通常表示其他内存已损坏
Ps2:
System.Diagnostics.Process.GetCurrentProcess()
在我的uwp c#项目中不存在 此Windows.System.Diagnostics
需要引用什么程序集才能可用?@JesseChisholm DLL是System.Diagnostics如果我没有弄错的话,默认情况下在我使用过的所有Visual Studio中都会启用它,Windows
命名空间是UWP的一部分。您应该在任何UWP项目中自动引用它。获取“尝试读取或写入受保护内存。这通常表示其他内存已损坏”的异常。GetForCurrentProcess()调用的持续时间(vs 2017 UWP目标版本构建17763)我尝试从c#调用windows32 api GetCurrentProcessId()。