markus / AppDemo / VistaSecurity.cs @ 950e6b84
이력 | 보기 | 이력해설 | 다운로드 (1.71 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.Security.Principal; |
7 |
using System.Windows.Forms; |
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 |
|
36 |
/// <summary> |
37 |
/// Restart the current process with administrator credentials |
38 |
/// </summary> |
39 |
internal static void RestartElevated() |
40 |
{ |
41 |
ProcessStartInfo startInfo = new ProcessStartInfo(); |
42 |
startInfo.UseShellExecute = true; |
43 |
startInfo.WorkingDirectory = Environment.CurrentDirectory; |
44 |
startInfo.FileName = Application.ExecutablePath; |
45 |
startInfo.Verb = "runas"; |
46 |
try |
47 |
{ |
48 |
Process p = Process.Start(startInfo); |
49 |
} |
50 |
catch(System.ComponentModel.Win32Exception ex) |
51 |
{ |
52 |
return; //If cancelled, do nothing |
53 |
} |
54 |
|
55 |
Application.Exit(); |
56 |
} |
57 |
} |
58 |
} |