프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / VistaSecurity.cs @ 1d79913e

이력 | 보기 | 이력해설 | 다운로드 (2.03 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Runtime.InteropServices;
5
using System.Diagnostics;
6
using System.Windows.Forms;
7
using System.Security.Principal;
8

    
9
namespace KCOM
10
{
11
    public static class VistaSecurity
12
    {
13
        [DllImport("user32")]
14
        public static extern UInt32 SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam);
15

    
16
        internal const int BCM_FIRST = 0x1600;
17
        internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C);
18

    
19
        static internal bool IsVistaOrHigher()
20
        {
21
            return Environment.OSVersion.Version.Major < 6;
22
        }
23

    
24
        /// <summary>
25
        /// Checks if the process is elevated
26
        /// </summary>
27
        /// <returns>If is elevated</returns>
28
        static internal bool IsAdmin()
29
        {
30
            WindowsIdentity id = WindowsIdentity.GetCurrent();
31
            WindowsPrincipal p = new WindowsPrincipal(id);
32
            return p.IsInRole(WindowsBuiltInRole.Administrator);
33
        }
34

    
35
        /// <summary>
36
        /// Add a shield icon to a button
37
        /// </summary>
38
        /// <param name="b">The button</param>
39
        static internal void AddShieldToButton(Button b)
40
        {
41
            b.FlatStyle = FlatStyle.System;
42
            SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF);
43
        }
44

    
45
        /// <summary>
46
        /// Restart the current process with administrator credentials
47
        /// </summary>
48
        internal static void RestartElevated()
49
        {
50
            ProcessStartInfo startInfo = new ProcessStartInfo();
51
            startInfo.UseShellExecute = true;
52
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
53
            startInfo.FileName = Application.ExecutablePath;
54
            startInfo.Verb = "runas";
55
            try
56
            {
57
                Process p = Process.Start(startInfo);
58
            }
59
            catch(System.ComponentModel.Win32Exception ex)
60
            {
61
                return; //If cancelled, do nothing
62
            }
63

    
64
            Application.Exit();
65
        }
66
    }
67
}
클립보드 이미지 추가 (최대 크기: 500 MB)