C# 判断程序是否以管理员身份运行

C# 判断程序是否以管理员身份运行, C# 编写的桌面程序,有些操作是需要进行提权的,如下:

而在代码中可以通过 以下代码来识别软件是否已提权

public static Boolean IsAdministrator() {
      System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
      System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
      return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
}

参考:

C# Check if run as administrator [duplicate]

推荐阅读:

Run as administrator C#

How do I force my .NET application to run as administrator?

Visual Studio 查看当前程序所有线程