markus / MarkusAutoUpdate / src / NetSparkle.Samples.NetFramework.WPF / VistaSecurity.cs @ 42d49521
이력 | 보기 | 이력해설 | 다운로드 (2.15 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 |
public static class VistaSecurity |
10 |
{ |
11 |
[DllImport("user32")] |
12 |
public static extern UInt32 SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam); |
13 |
|
14 |
internal const int BCM_FIRST = 0x1600; |
15 |
internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C); |
16 |
|
17 |
static internal bool IsVistaOrHigher() |
18 |
{ |
19 |
return Environment.OSVersion.Version.Major < 6; |
20 |
} |
21 |
|
22 |
/// <summary> |
23 |
/// Checks if the process is elevated |
24 |
/// </summary> |
25 |
/// <returns>If is elevated</returns> |
26 |
static internal bool IsAdmin() |
27 |
{ |
28 |
WindowsIdentity id = WindowsIdentity.GetCurrent(); |
29 |
WindowsPrincipal p = new WindowsPrincipal(id); |
30 |
return p.IsInRole(WindowsBuiltInRole.Administrator); |
31 |
} |
32 |
|
33 |
/// <summary> |
34 |
/// Add a shield icon to a button |
35 |
/// </summary> |
36 |
/// <param name="b">The button</param> |
37 |
static internal void AddShieldToButton(Button b) |
38 |
{ |
39 |
b.FlatStyle = FlatStyle.System; |
40 |
SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF); |
41 |
} |
42 |
|
43 |
/// <summary> |
44 |
/// Restart the current process with administrator credentials |
45 |
/// </summary> |
46 |
internal static void RestartElevated(string arguments) |
47 |
{ |
48 |
ProcessStartInfo startInfo = new ProcessStartInfo(); |
49 |
startInfo.UseShellExecute = true; |
50 |
startInfo.Arguments = arguments; |
51 |
startInfo.WorkingDirectory = Environment.CurrentDirectory; |
52 |
startInfo.FileName = Application.ExecutablePath; |
53 |
startInfo.Verb = "runas"; |
54 |
try |
55 |
{ |
56 |
Process p = Process.Start(startInfo); |
57 |
|
58 |
} |
59 |
catch(System.ComponentModel.Win32Exception ex) |
60 |
{ |
61 |
return; //If cancelled, do nothing |
62 |
} |
63 |
|
64 |
System.Windows.Forms.Application.Exit(); |
65 |
System.Windows.Application.Current.Shutdown(); |
66 |
} |
67 |
} |