File size: 1,085 Bytes
5f98228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace FastSeekWpf.Core;

public static class Elevation
{
    /// <summary>
    /// Returns true if the current process is running with Administrator privileges.
    /// </summary>
    public static bool IsElevated()
    {
        try
        {
            using var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// Returns a human-readable elevation status string.
    /// </summary>
    public static string StatusText()
    {
        if (IsElevated())
            return "Administrator (elevated)";

        try
        {
            using var identity = WindowsIdentity.GetCurrent();
            return $"{identity.Name} (NOT elevated)";
        }
        catch
        {
            return "Unknown (NOT elevated)";
        }
    }
}