mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 19:57:57 +01:00
Fix issue#24
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
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(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Wox.UAC.exe"),
|
||||
Arguments = args,
|
||||
CreateNoWindow = true,
|
||||
Verb = "runas"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var process = new Process();
|
||||
process.StartInfo = psi;
|
||||
process.Start();
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,6 @@
|
||||
<Compile Include="GloablHotkey.cs" />
|
||||
<Compile Include="HotkeyModel.cs" />
|
||||
<Compile Include="IniParser.cs" />
|
||||
<Compile Include="UAC.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="UserSettings\PluginHotkey.cs" />
|
||||
<Compile Include="UserSettings\UserSelectedRecords.cs" />
|
||||
|
||||
70
Wox.UAC/FileTypeAssociateInstaller.cs
Normal file
70
Wox.UAC/FileTypeAssociateInstaller.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using Microsoft.Win32;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.UAC
|
||||
{
|
||||
public class FileTypeAssociateInstaller
|
||||
{
|
||||
[DllImport("shell32.dll")]
|
||||
private static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
|
||||
|
||||
/// <summary>
|
||||
/// associate filetype with specified program
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="fileType"></param>
|
||||
/// <param name="iconPath"></param>
|
||||
/// <param name="overrides"></param>
|
||||
private static void SaveReg(string filePath, string fileType, string iconPath, bool overrides)
|
||||
{
|
||||
RegistryKey classRootKey = Registry.ClassesRoot.OpenSubKey("", true);
|
||||
RegistryKey woxKey = classRootKey.OpenSubKey(fileType, true);
|
||||
if (woxKey != null)
|
||||
{
|
||||
if (!overrides)
|
||||
{
|
||||
return;
|
||||
}
|
||||
classRootKey.DeleteSubKeyTree(fileType);
|
||||
}
|
||||
classRootKey.CreateSubKey(fileType);
|
||||
woxKey = classRootKey.OpenSubKey(fileType, true);
|
||||
woxKey.SetValue("", "wox.wox");
|
||||
woxKey.SetValue("Content Type", "application/wox");
|
||||
|
||||
RegistryKey iconKey = woxKey.CreateSubKey("DefaultIcon");
|
||||
iconKey.SetValue("", iconPath);
|
||||
|
||||
woxKey.CreateSubKey("shell");
|
||||
RegistryKey shellKey = woxKey.OpenSubKey("shell", true);
|
||||
shellKey.SetValue("", "Open");
|
||||
RegistryKey openKey = shellKey.CreateSubKey("open");
|
||||
openKey.SetValue("", "Open with wox");
|
||||
|
||||
openKey = shellKey.OpenSubKey("open", true);
|
||||
openKey.CreateSubKey("command");
|
||||
RegistryKey commandKey = openKey.OpenSubKey("command", true);
|
||||
string pathString = "\"" + filePath + "\" \"installPlugin\" \"%1\"";
|
||||
commandKey.SetValue("", pathString);
|
||||
|
||||
//refresh cache
|
||||
SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
|
||||
public void RegisterInstaller()
|
||||
{
|
||||
string filePath = Directory.GetCurrentDirectory() + "\\Wox.exe";
|
||||
string iconPath = Directory.GetCurrentDirectory() + "\\app.ico";
|
||||
|
||||
SaveReg(filePath, ".wox", iconPath, true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace Wox.UAC
|
||||
{
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
PluginInstaller installer = new PluginInstaller();
|
||||
FileTypeAssociateInstaller installer = new FileTypeAssociateInstaller();
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
@@ -16,32 +16,12 @@ namespace Wox.UAC
|
||||
{
|
||||
switch (param[1])
|
||||
{
|
||||
case "UAC":
|
||||
Invoke(param[2], param[3], param[4]);
|
||||
break;
|
||||
|
||||
case "AssociatePluginInstaller":
|
||||
installer.RegisterInstaller();
|
||||
break;
|
||||
|
||||
case "InstallPlugin":
|
||||
var path = param[2];
|
||||
installer.Install(path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Application.Current.Shutdown(0);
|
||||
}
|
||||
|
||||
private static void Invoke(string namespaceName, string className, string methodName)
|
||||
{
|
||||
Type type = Type.GetType(namespaceName + "." + className + "," + namespaceName);
|
||||
if (type != null)
|
||||
{
|
||||
object instance = Activator.CreateInstance(type);
|
||||
MethodInfo method = type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (method != null) method.Invoke(instance, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="PluginInstaller.cs" />
|
||||
<Compile Include="FileTypeAssociateInstaller.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
|
||||
@@ -1,72 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using Microsoft.Win32;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.UAC
|
||||
namespace Wox.Helper
|
||||
{
|
||||
public class PluginInstaller
|
||||
{
|
||||
[DllImport("shell32.dll")]
|
||||
private static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
|
||||
|
||||
/// <summary>
|
||||
/// associate filetype with specified program
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="fileType"></param>
|
||||
/// <param name="iconPath"></param>
|
||||
/// <param name="overrides"></param>
|
||||
private static void SaveReg(string filePath, string fileType, string iconPath, bool overrides)
|
||||
{
|
||||
RegistryKey classRootKey = Registry.ClassesRoot.OpenSubKey("", true);
|
||||
RegistryKey woxKey = classRootKey.OpenSubKey(fileType, true);
|
||||
if (woxKey != null)
|
||||
{
|
||||
if (!overrides)
|
||||
{
|
||||
return;
|
||||
}
|
||||
classRootKey.DeleteSubKeyTree(fileType);
|
||||
}
|
||||
classRootKey.CreateSubKey(fileType);
|
||||
woxKey = classRootKey.OpenSubKey(fileType, true);
|
||||
woxKey.SetValue("", "wox.wox");
|
||||
woxKey.SetValue("Content Type", "application/wox");
|
||||
|
||||
RegistryKey iconKey = woxKey.CreateSubKey("DefaultIcon");
|
||||
iconKey.SetValue("", iconPath);
|
||||
|
||||
woxKey.CreateSubKey("shell");
|
||||
RegistryKey shellKey = woxKey.OpenSubKey("shell", true);
|
||||
shellKey.SetValue("", "Open");
|
||||
RegistryKey openKey = shellKey.CreateSubKey("open");
|
||||
openKey.SetValue("", "Open with wox");
|
||||
|
||||
openKey = shellKey.OpenSubKey("open", true);
|
||||
openKey.CreateSubKey("command");
|
||||
RegistryKey commandKey = openKey.OpenSubKey("command", true);
|
||||
string pathString = "\"" + filePath + "\" \"installPlugin\" \"%1\"";
|
||||
commandKey.SetValue("", pathString);
|
||||
|
||||
//refresh cache
|
||||
SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);
|
||||
}
|
||||
|
||||
public void RegisterInstaller()
|
||||
{
|
||||
string filePath = Directory.GetCurrentDirectory() + "\\Wox.UAC.exe";
|
||||
string iconPath = Directory.GetCurrentDirectory() + "\\app.ico";
|
||||
|
||||
SaveReg(filePath, ".wox", iconPath, true);
|
||||
}
|
||||
|
||||
public void Install(string path)
|
||||
public static void Install(string path)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
@@ -149,7 +97,7 @@ namespace Wox.UAC
|
||||
}
|
||||
|
||||
private static PluginMetadata GetMetadataFromJson(string pluginDirectory)
|
||||
{
|
||||
{
|
||||
string configPath = Path.Combine(pluginDirectory, "plugin.json");
|
||||
PluginMetadata metadata;
|
||||
|
||||
@@ -208,7 +156,7 @@ namespace Wox.UAC
|
||||
/// <param name="zipedFile">The ziped file.</param>
|
||||
/// <param name="strDirectory">The STR directory.</param>
|
||||
/// <param name="overWrite">overwirte</param>
|
||||
private void UnZip(string zipedFile, string strDirectory, bool overWrite)
|
||||
private static void UnZip(string zipedFile, string strDirectory, bool overWrite)
|
||||
{
|
||||
if (strDirectory == "")
|
||||
strDirectory = Directory.GetCurrentDirectory();
|
||||
@@ -230,6 +230,16 @@ namespace Wox
|
||||
case "hidestart":
|
||||
HideApp();
|
||||
break;
|
||||
|
||||
case "installplugin":
|
||||
var path = args[1];
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
MessageBox.Show("Plugin " + path + " didn't exist");
|
||||
return;
|
||||
}
|
||||
PluginInstaller.Install(path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,9 @@
|
||||
<TextBlock Text="Theme:" />
|
||||
<ComboBox x:Name="themeComboBox" SelectionChanged="ThemeComboBox_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="10">
|
||||
<Button x:Name="btnEnableInstaller" Click="BtnEnableInstaller_OnClick">enable plugin installer</Button>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</TabItem>
|
||||
<TabItem Header="Hotkey">
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
@@ -208,6 +209,9 @@ namespace Wox
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
private void BtnEnableInstaller_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Process.Start("Wox.UAC.exe", "AssociatePluginInstaller");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,6 +130,7 @@
|
||||
</Compile>
|
||||
<Compile Include="DispatcherExtensions.cs" />
|
||||
<Compile Include="Helper\Log.cs" />
|
||||
<Compile Include="Helper\PluginInstaller.cs" />
|
||||
<Compile Include="Helper\WoxException.cs" />
|
||||
<Compile Include="Helper\WoxPythonException.cs" />
|
||||
<Compile Include="HotkeyControl.xaml.cs">
|
||||
|
||||
Reference in New Issue
Block a user