[C#] UAC

  1. Check Windows Vista or Windows 7 (Enabled UAC)
  2. using System;
    
    private bool IsUAC()
    {
        OperatingSystem osInfo = Environment.OSVersion;
        if (osInfo.Platform == PlatformID.Win32NT)
        {
            if (osInfo.Version.Major == 6)
            {
                if (osInfo.Version.Minor == 0)
                {
                    // Windows Vista, Windows Server 2008
                    return true;
                }
                else if (osInfo.Version.Minor == 1)
                {
                    // Windows 7, Windows Server 2008 R2
                    return true;
                }
            }
            else if (osInfo.Version.Major > 6)
            {
                // new Windows
                return true;
            }
        }
    
        return false;
    }
  3. RUN as Administrator?
  4. using System.Security.Principal;
    
    private bool IsAdministrator()
    {
        bool isAllowed = false;
    
        try
        {
            WindowsIdentity wi = WindowsIdentity.GetCurrent();
            WindowsPrincipal wp = new WindowsPrincipal(wi);
            isAllowed = wp.IsInRole(WindowsBuiltInRole.Administrator);
        }
        catch
        {
            isAllowed = false;
        }
    
        return isAllowed;
    }
  5. Administrator group?
  6. using System.DirectoryServices.AccountManagement;
    
    private bool IsAdministrators()
    {
        bool isAllowed = false;
    
        try
        {
            PrincipalContext pc = new PrincipalContext(ContextType.Machine, null);
            UserPrincipal up = UserPrincipal.Current;
            foreach (GroupPrincipal gp in up.GetGroups())
            {
                try
                {
                    if (gp.Name.ToLower().Equals("Administrators".ToLower()))
                    {
                        isAllowed = true;
                    }
                }
                finally
                {
                    gp.Dispose();
                }
            }
        }
        catch
        {
            isAllowed = false;
        }
    
        return isAllowed;
    }
  7. Restart Application as Administrator
  8. using System;
    using System.Diagnostics;
    
    private void RestartApplication()
    {
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.UseShellExecute = true;
        psi.WorkingDirectory = Environment.CurrentDirectory;
        psi.FileName = Application.ExecutablePath;
        psi.Verb = "runas";
    
        try
        {
            Process p = Process.Start(psi);
        }
        catch
        {
            return;
        }
    
        Application.Exit();
    }
  9. Display UAC icon
  10. using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace Sample
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.dll")]
            private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
            uint BCM_SETSHIELD = 0x0000160C;
            public Form1()
            {
                InitializeComponent();
                button1.FlatStyle = FlatStyle.System;
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                HandleRef hwnd = new HandleRef(button1, button1.Handle);
                if (checkBox1.Checked)
                {
                    SendMessage(hwnd, BCM_SETSHIELD, new IntPtr(0), new IntPtr(1));
                }
                else
                {
                    SendMessage(hwnd, BCM_SETSHIELD, new IntPtr(0), new IntPtr(0));
                }
            }
        }
    }
Advertisements