Skip to content

Commit c279f40

Browse files
committed
Add run as different user option when file is of certain type
1 parent 725eee7 commit c279f40

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Plugins/Wox.Plugin.Folder/ContextMenuLoader.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Threading.Tasks;
56
using System.Windows;
67
using Wox.Infrastructure.Logger;
8+
using Wox.Plugin.SharedCommands;
79

810
namespace Wox.Plugin.Folder
911
{
@@ -100,6 +102,27 @@ public List<Result> LoadContextMenus(Result selectedResult)
100102
IcoPath = icoPath
101103
});
102104

105+
if (record.Type == ResultType.File && CanRunAsDifferentUser(record.FullPath))
106+
contextMenus.Add(new Result
107+
{
108+
Title = "Run as different user",
109+
Action = (context) =>
110+
{
111+
try
112+
{
113+
Task.Run(()=> ShellCommand.RunAsDifferentUser(record.FullPath.SetProcessStartInfo()));
114+
}
115+
catch (FileNotFoundException e)
116+
{
117+
var name = "Plugin: Folder";
118+
var message = $"File not found: {e.Message}";
119+
_context.API.ShowMsg(name, message);
120+
}
121+
122+
return true;
123+
},
124+
IcoPath = "Images/user.png"
125+
});
103126
}
104127

105128
return contextMenus;
@@ -162,6 +185,20 @@ public void LogException(string message, Exception e)
162185
{
163186
Log.Exception($"|Wox.Plugin.Folder.ContextMenu|{message}", e);
164187
}
188+
189+
private bool CanRunAsDifferentUser(string path)
190+
{
191+
switch(Path.GetExtension(path))
192+
{
193+
case ".exe":
194+
case ".bat":
195+
return true;
196+
197+
default:
198+
return false;
199+
200+
}
201+
}
165202
}
166203

167204
public class SearchResult
64.1 KB
Loading

Plugins/Wox.Plugin.Folder/Wox.Plugin.Folder.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
<Content Include="Images\file.png">
8585
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8686
</Content>
87+
<None Include="Images\user.png">
88+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
89+
</None>
8790
<Content Include="Languages\en.xaml">
8891
<Generator>MSBuild:Compile</Generator>
8992
<SubType>Designer</SubType>

0 commit comments

Comments
 (0)