mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
Remove ShellRun
1. Remove ShellRun using p/invoke 2. Remove ResolveShortcut when open contanning folder 3. fix #88
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -64,7 +65,7 @@ namespace Wox.Plugin.CMD
|
||||
IcoPath = "Images/cmd.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
ExecuteCmd(m);
|
||||
ExecuteCMD(m);
|
||||
return true;
|
||||
}
|
||||
}));
|
||||
@@ -97,7 +98,7 @@ namespace Wox.Plugin.CMD
|
||||
IcoPath = "Images/cmd.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
ExecuteCmd(m.Key);
|
||||
ExecuteCMD(m.Key);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
@@ -116,7 +117,7 @@ namespace Wox.Plugin.CMD
|
||||
IcoPath = "Images/cmd.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
ExecuteCmd(cmd);
|
||||
ExecuteCMD(cmd);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
@@ -134,21 +135,32 @@ namespace Wox.Plugin.CMD
|
||||
IcoPath = "Images/cmd.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
ExecuteCmd(m.Key);
|
||||
ExecuteCMD(m.Key);
|
||||
return true;
|
||||
}
|
||||
}).Take(5);
|
||||
return history.ToList();
|
||||
}
|
||||
|
||||
private void ExecuteCmd(string cmd, bool runAsAdministrator = false)
|
||||
private void ExecuteCMD(string cmd, bool runAsAdministrator = false)
|
||||
{
|
||||
var fullCmd = CMDStorage.Instance.LeaveCmdOpen ? $"cmd /k \"{cmd}\" & pause & exit" : cmd;
|
||||
var success = context.API.ShellRun(fullCmd, runAsAdministrator);
|
||||
if (success)
|
||||
var arguments = CMDStorage.Instance.LeaveCmdOpen ? $"/k {cmd}" : $"/c {cmd} & pause";
|
||||
var info = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = true,
|
||||
FileName = "cmd.exe",
|
||||
Arguments = arguments,
|
||||
Verb = runAsAdministrator ? "runas" : ""
|
||||
};
|
||||
try
|
||||
{
|
||||
Process.Start(info);
|
||||
CMDStorage.Instance.AddCmdHistory(cmd);
|
||||
}
|
||||
catch (FileNotFoundException e)
|
||||
{
|
||||
MessageBox.Show($"Command not found: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
@@ -215,7 +227,7 @@ namespace Wox.Plugin.CMD
|
||||
Action = c =>
|
||||
{
|
||||
context.API.HideApp();
|
||||
ExecuteCmd(selectedResult.Title, true);
|
||||
ExecuteCMD(selectedResult.Title, true);
|
||||
return true;
|
||||
},
|
||||
IcoPath = "Images/cmd.png"
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.ServiceProcess;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Plugin.Everything.Everything;
|
||||
|
||||
namespace Wox.Plugin.Everything
|
||||
@@ -57,7 +56,11 @@ namespace Wox.Plugin.Everything
|
||||
r.Action = (c) =>
|
||||
{
|
||||
context.API.HideApp();
|
||||
context.API.ShellRun(path);
|
||||
Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = path,
|
||||
UseShellExecute = true
|
||||
});
|
||||
return true;
|
||||
};
|
||||
r.ContextData = s;
|
||||
@@ -124,12 +127,12 @@ namespace Wox.Plugin.Everything
|
||||
{
|
||||
List<ContextMenu> defaultContextMenus = new List<ContextMenu>();
|
||||
ContextMenu openFolderContextMenu = new ContextMenu()
|
||||
{
|
||||
Name = context.API.GetTranslation("wox_plugin_everything_open_containing_folder"),
|
||||
Command = "explorer.exe",
|
||||
Argument = " /select,\"{path}\"",
|
||||
ImagePath = "Images\\folder.png"
|
||||
};
|
||||
{
|
||||
Name = context.API.GetTranslation("wox_plugin_everything_open_containing_folder"),
|
||||
Command = "explorer.exe",
|
||||
Argument = " /select,\"{path}\"",
|
||||
ImagePath = "Images\\folder.png"
|
||||
};
|
||||
|
||||
defaultContextMenus.Add(openFolderContextMenu);
|
||||
return defaultContextMenus;
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Wox.Plugin.Program
|
||||
Action = (e) =>
|
||||
{
|
||||
context.API.HideApp();
|
||||
context.API.ShellRun(c.ExecutePath);
|
||||
Process.Start(c.ExecutePath);
|
||||
return true;
|
||||
}
|
||||
}).ToList();
|
||||
@@ -198,7 +198,11 @@ namespace Wox.Plugin.Program
|
||||
Action = _ =>
|
||||
{
|
||||
context.API.HideApp();
|
||||
context.API.ShellRun(p.ExecutePath, true);
|
||||
Process.Start( new ProcessStartInfo
|
||||
{
|
||||
FileName = p.ExecutePath,
|
||||
Verb = "runas"
|
||||
});
|
||||
return true;
|
||||
},
|
||||
IcoPath = "Images/cmd.png"
|
||||
@@ -209,19 +213,10 @@ namespace Wox.Plugin.Program
|
||||
Action = _ =>
|
||||
{
|
||||
context.API.HideApp();
|
||||
String Path = p.ExecutePath;
|
||||
//check if shortcut
|
||||
if (Path.EndsWith(".lnk"))
|
||||
{
|
||||
//get location of shortcut
|
||||
var resolved = ShortcutHelper.ResolveShortcut(Path);
|
||||
if(!string.IsNullOrEmpty(resolved))
|
||||
Path = resolved;
|
||||
}
|
||||
//get parent folder
|
||||
Path = Directory.GetParent(Path).FullName;
|
||||
var folderPath = Directory.GetParent(p.ExecutePath).FullName;
|
||||
//open the folder
|
||||
context.API.ShellRun("explorer.exe " + Path, false);
|
||||
Process.Start(folderPath);
|
||||
return true;
|
||||
},
|
||||
IcoPath = "Images/folder.png"
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
Shortcut resolver. Avoids using IWshRuntimeLibrary
|
||||
Proposed by Sam Saffron @ StackOverflow
|
||||
*/
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Wox.Plugin.Program
|
||||
{
|
||||
public class ShortcutHelper
|
||||
{
|
||||
#region Signatures
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
struct WIN32_FIND_DATAW
|
||||
{
|
||||
public uint dwFileAttributes;
|
||||
public long ftCreationTime;
|
||||
public long ftLastAccessTime;
|
||||
public long ftLastWriteTime;
|
||||
public uint nFileSizeHigh;
|
||||
public uint nFileSizeLow;
|
||||
public uint dwReserved0;
|
||||
public uint dwReserved1;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
|
||||
public string cFileName;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
|
||||
public string cAlternateFileName;
|
||||
}
|
||||
|
||||
/// <summary>The IShellLink interface allows Shell links to be created, modified, and resolved</summary>
|
||||
[ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
|
||||
interface IShellLinkW
|
||||
{
|
||||
/// <summary>Retrieves the path and file name of a Shell link object</summary>
|
||||
void GetPath([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATAW pfd, uint fFlags);
|
||||
}
|
||||
|
||||
[ComImport, Guid("0000010c-0000-0000-c000-000000000046"),
|
||||
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface IPersist { }
|
||||
|
||||
[ComImport, Guid("0000010b-0000-0000-C000-000000000046"),
|
||||
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface IPersistFile : IPersist
|
||||
{
|
||||
new void GetClassID(out Guid pClassID);
|
||||
[PreserveSig]
|
||||
void Load([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName, uint dwMode);
|
||||
}
|
||||
|
||||
const uint STGM_READ = 0;
|
||||
const int MAX_PATH = 260;
|
||||
|
||||
// CLSID_ShellLink from ShlGuid.h
|
||||
[
|
||||
ComImport(),
|
||||
Guid("00021401-0000-0000-C000-000000000046")
|
||||
]
|
||||
public class ShellLink
|
||||
{
|
||||
}
|
||||
#endregion
|
||||
public static string ResolveShortcut(string filename)
|
||||
{
|
||||
ShellLink link = new ShellLink();
|
||||
((IPersistFile)link).Load(filename, STGM_READ);
|
||||
StringBuilder sb = new StringBuilder(MAX_PATH);
|
||||
WIN32_FIND_DATAW data = new WIN32_FIND_DATAW();
|
||||
((IShellLinkW)link).GetPath(sb, sb.Capacity, out data, 0);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,6 @@
|
||||
<Compile Include="ProgramSuffixes.xaml.cs">
|
||||
<DependentUpon>ProgramSuffixes.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ShortcutHelper.cs" />
|
||||
<Compile Include="StringEmptyConverter.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user