Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在UWP中获取当前进程ID?_C#_Uwp - Fatal编程技术网

C# 如何在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

使用.Net Framework,可以在以下位置实现:

但是如何在UWP中获得当前进程ID?
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()。