Testing application for Administrative Running Rights

后端 未结 2 1928
一生所求
一生所求 2021-02-15 10:12

I want a sure-shot method to test if the application was run via the UAC box and has full administrative rights. Earlier, I thought of making a folder in C:\\Windows\\ for testi

2条回答
  •  日久生厌
    2021-02-15 10:57

    C#:

    using System.Security.Principal;
    
    ...
    
    var identity = WindowsIdentity.GetCurrent();
    var principal = new WindowsPrincipal(identity);
    bool isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
    

    VB.Net:

    Imports System.Security.Principal
    
    ...
    
    Dim identity = WindowsIdentity.GetCurrent()
    Dim principal = new WindowsPrincipal(identity)
    Dim isElevated as Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator)
    

提交回复
热议问题