mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Add start Wox on system startup config & code refactorying.
This commit is contained in:
96
Wox.Infrastructure/UAC.cs
Normal file
96
Wox.Infrastructure/UAC.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Media.Imaging;
|
||||
using Size = System.Drawing.Size;
|
||||
|
||||
namespace Wox.Infrastructure
|
||||
{
|
||||
|
||||
public static class UAC
|
||||
{
|
||||
/// <summary>
|
||||
/// Execute methods that require Admin role, which will popup UAC window.
|
||||
///
|
||||
/// Notes:
|
||||
/// 1. Invoker method shouldn't have any parameters
|
||||
/// 2. Add attribute [MethodImpl(MethodImplOptions.NoInlining)] to invoker method
|
||||
///
|
||||
/// Example:
|
||||
/// [MethodImpl(MethodImplOptions.NoInlining)]
|
||||
/// private void OnStartWithWindowUnChecked()
|
||||
/// {
|
||||
/// UAC.ExecuteAdminMethod(() => SetStartup(false));
|
||||
/// }
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="method"></param>
|
||||
public static void ExecuteAdminMethod(Action method)
|
||||
{
|
||||
if (method == null) return;
|
||||
if (Environment.OSVersion.Version.Major <= 5 || IsAdministrator())
|
||||
{
|
||||
method();
|
||||
return;
|
||||
}
|
||||
|
||||
StackTrace stackTrace = new StackTrace();
|
||||
// Get calling method name
|
||||
MethodBase callingMethod = stackTrace.GetFrame(1).GetMethod();
|
||||
string methodName = callingMethod.Name;
|
||||
if (callingMethod.ReflectedType == null) return;
|
||||
|
||||
string className = callingMethod.ReflectedType.Name;
|
||||
string nameSpace = callingMethod.ReflectedType.Namespace;
|
||||
string args = string.Format("UAC {0} {1} {2}", nameSpace,className,methodName);
|
||||
Debug.WriteLine(args);
|
||||
var psi = new ProcessStartInfo
|
||||
{
|
||||
FileName = Path.Combine(Directory.GetCurrentDirectory(), "Wox.UAC.exe"),
|
||||
Arguments = args,
|
||||
CreateNoWindow = true,
|
||||
Verb = "runas"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var process = new Process();
|
||||
process.StartInfo = psi;
|
||||
process.Start();
|
||||
process.WaitForExit();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
MessageBox.Show("Execute failed: " + e);
|
||||
#if (DEBUG)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static bool IsAdministrator()
|
||||
{
|
||||
var identity = WindowsIdentity.GetCurrent();
|
||||
if (identity != null)
|
||||
{
|
||||
var principal = new WindowsPrincipal(identity);
|
||||
return principal.IsInRole(WindowsBuiltInRole.Administrator);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user