Skip to content

Commit d2ac020

Browse files
authored
Modify context menu options for executable and installer files (#1155)
1 parent af02b66 commit d2ac020

20 files changed

+246
-21
lines changed

Files.Launcher/Program.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,30 @@ private static void HandleApplicationLaunch(string application, AppServiceReques
316316
process.StartInfo.FileName = application;
317317
// Show window if workingDirectory (opening terminal)
318318
process.StartInfo.CreateNoWindow = string.IsNullOrEmpty(workingDirectory);
319-
process.StartInfo.Arguments = arguments;
319+
if (arguments == "runas")
320+
{
321+
process.StartInfo.UseShellExecute = true;
322+
process.StartInfo.Verb = "runas";
323+
if (Path.GetExtension(application).ToLower() == ".msi")
324+
{
325+
process.StartInfo.FileName = "msiexec.exe";
326+
process.StartInfo.Arguments = $"/a \"{application}\"";
327+
}
328+
}
329+
else if (arguments == "runasuser")
330+
{
331+
process.StartInfo.UseShellExecute = true;
332+
process.StartInfo.Verb = "runasuser";
333+
if (Path.GetExtension(application).ToLower() == ".msi")
334+
{
335+
process.StartInfo.FileName = "msiexec.exe";
336+
process.StartInfo.Arguments = $"/i \"{application}\"";
337+
}
338+
}
339+
else
340+
{
341+
process.StartInfo.Arguments = arguments;
342+
}
320343
process.StartInfo.WorkingDirectory = workingDirectory;
321344
process.Start();
322345
}
@@ -430,4 +453,4 @@ private static void Connection_ServiceClosed(AppServiceConnection sender, AppSer
430453
appServiceExit.Set();
431454
}
432455
}
433-
}
456+
}

Files.sln

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Global
5050
{533F9E86-EE0A-4FCB-B70C-F29532C1B787}.Release|x86.ActiveCfg = Release|x86
5151
{533F9E86-EE0A-4FCB-B70C-F29532C1B787}.Release|x86.Build.0 = Release|x86
5252
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|Any CPU.ActiveCfg = Debug|x86
53+
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|Any CPU.Build.0 = Debug|x86
5354
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|ARM.ActiveCfg = Debug|ARM
5455
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|ARM.Build.0 = Debug|ARM
5556
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|ARM.Deploy.0 = Debug|ARM
@@ -58,7 +59,6 @@ Global
5859
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|ARM64.Deploy.0 = Debug|ARM64
5960
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|x64.ActiveCfg = Debug|x64
6061
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|x64.Build.0 = Debug|x64
61-
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|x64.Deploy.0 = Debug|x64
6262
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|x86.ActiveCfg = Debug|x86
6363
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|x86.Build.0 = Debug|x86
6464
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Debug|x86.Deploy.0 = Debug|x86
@@ -76,6 +76,8 @@ Global
7676
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Release|x86.Build.0 = Release|x86
7777
{64C30C4E-A69A-411C-9F78-776E7AAD583C}.Release|x86.Deploy.0 = Release|x86
7878
{3B15596C-3DB9-4B58-B4C8-442D06A8C130}.Debug|Any CPU.ActiveCfg = Debug|x86
79+
{3B15596C-3DB9-4B58-B4C8-442D06A8C130}.Debug|Any CPU.Build.0 = Debug|x86
80+
{3B15596C-3DB9-4B58-B4C8-442D06A8C130}.Debug|Any CPU.Deploy.0 = Debug|x86
7981
{3B15596C-3DB9-4B58-B4C8-442D06A8C130}.Debug|ARM.ActiveCfg = Debug|ARM
8082
{3B15596C-3DB9-4B58-B4C8-442D06A8C130}.Debug|ARM.Build.0 = Debug|ARM
8183
{3B15596C-3DB9-4B58-B4C8-442D06A8C130}.Debug|ARM.Deploy.0 = Debug|ARM
@@ -142,6 +144,7 @@ Global
142144
{0533133F-2559-4B53-A0FD-0970BC0E312E}.Release|x86.ActiveCfg = Release|Any CPU
143145
{0533133F-2559-4B53-A0FD-0970BC0E312E}.Release|x86.Build.0 = Release|Any CPU
144146
{D4E6A3AB-DF72-44A4-9ACB-C8A222656A20}.Debug|Any CPU.ActiveCfg = Debug|Win32
147+
{D4E6A3AB-DF72-44A4-9ACB-C8A222656A20}.Debug|Any CPU.Build.0 = Debug|Win32
145148
{D4E6A3AB-DF72-44A4-9ACB-C8A222656A20}.Debug|ARM.ActiveCfg = Debug|ARM
146149
{D4E6A3AB-DF72-44A4-9ACB-C8A222656A20}.Debug|ARM.Build.0 = Debug|ARM
147150
{D4E6A3AB-DF72-44A4-9ACB-C8A222656A20}.Debug|ARM64.ActiveCfg = Debug|ARM64

Files/BaseLayout.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public void RightClickContextMenu_Opening(object sender, object e)
336336
}
337337
}
338338

339-
public void RightClickItemContextMenu_Opening(object sender, object e)
339+
public async void RightClickItemContextMenu_Opening(object sender, object e)
340340
{
341341

342342
SetShellContextmenu();
@@ -355,20 +355,45 @@ public void RightClickItemContextMenu_Opening(object sender, object e)
355355

356356
if (!string.IsNullOrEmpty(selectedDataItem.FileExtension))
357357
{
358-
if (selectedDataItem.FileExtension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
358+
if (SelectedItem.FileExtension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
359359
{
360360
UnloadMenuFlyoutItemByName("OpenItem");
361361
UnloadMenuFlyoutItemByName("OpenItemWithAppPicker");
362+
UnloadMenuFlyoutItemByName("RunAsAdmin");
363+
UnloadMenuFlyoutItemByName("RunAsAnotherUser");
362364
(this.FindName("UnzipItem") as MenuFlyoutItemBase).Visibility = Visibility.Visible;
363-
//this.FindName("UnzipItem");
364365
}
365-
else if (!selectedDataItem.FileExtension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
366+
else if (SelectedItem.FileExtension.Equals(".exe", StringComparison.OrdinalIgnoreCase)
367+
|| SelectedItem.FileExtension.Equals(".msi", StringComparison.OrdinalIgnoreCase)
368+
|| SelectedItem.FileExtension.Equals(".bat", StringComparison.OrdinalIgnoreCase))
369+
{
370+
(this.FindName("OpenItem") as MenuFlyoutItemBase).Visibility = Visibility.Visible;
371+
//this.FindName("OpenItem");
372+
373+
UnloadMenuFlyoutItemByName("OpenItemWithAppPicker");
374+
UnloadMenuFlyoutItemByName("UnzipItem");
375+
(this.FindName("RunAsAdmin") as MenuFlyoutItemBase).Visibility = Visibility.Visible;
376+
(this.FindName("RunAsAnotherUser") as MenuFlyoutItemBase).Visibility = Visibility.Visible;
377+
}
378+
else if (SelectedItem.FileExtension.Equals(".appx", StringComparison.OrdinalIgnoreCase)
379+
|| SelectedItem.FileExtension.Equals(".msix", StringComparison.OrdinalIgnoreCase)
380+
|| SelectedItem.FileExtension.Equals(".appxbundle", StringComparison.OrdinalIgnoreCase)
381+
|| SelectedItem.FileExtension.Equals(".msixbundle", StringComparison.OrdinalIgnoreCase))
382+
{
383+
(this.FindName("OpenItemWithAppPicker") as MenuFlyoutItemBase).Visibility = Visibility.Visible;
384+
UnloadMenuFlyoutItemByName("UnzipItem");
385+
UnloadMenuFlyoutItemByName("RunAsAdmin");
386+
UnloadMenuFlyoutItemByName("RunAsAnotherUser");
387+
}
388+
else
366389
{
367390
(this.FindName("OpenItem") as MenuFlyoutItemBase).Visibility = Visibility.Visible;
368391
//this.FindName("OpenItem");
369392

370393
(this.FindName("OpenItemWithAppPicker") as MenuFlyoutItemBase).Visibility = Visibility.Visible;
371394
UnloadMenuFlyoutItemByName("UnzipItem");
395+
UnloadMenuFlyoutItemByName("RunAsAdmin");
396+
UnloadMenuFlyoutItemByName("RunAsAnotherUser");
372397
}
373398
}
374399
}

Files/Files.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,14 +576,14 @@
576576
<Name>Files.MessageRelay</Name>
577577
</ProjectReference>
578578
</ItemGroup>
579+
<ItemGroup>
580+
<Folder Include="Commands\" />
581+
</ItemGroup>
579582
<ItemGroup>
580583
<SDKReference Include="WindowsDesktop, Version=10.0.19041.0">
581584
<Name>Windows Desktop Extensions for the UWP</Name>
582585
</SDKReference>
583586
</ItemGroup>
584-
<ItemGroup>
585-
<Folder Include="Commands\" />
586-
</ItemGroup>
587587
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
588588
<VisualStudioVersion>14.0</VisualStudioVersion>
589589
</PropertyGroup>

Files/Interacts/Interaction.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
using Windows.UI.Xaml.Media;
4242
using Windows.UI.Xaml.Media.Animation;
4343
using static Files.Dialogs.ConfirmDeleteDialog;
44+
using Windows.Security.Credentials.UI;
4445

4546
namespace Files.Interacts
4647
{
@@ -174,19 +175,27 @@ public void GetPath_Click(object sender, RoutedEventArgs e)
174175
}
175176
}
176177

177-
public static async Task InvokeWin32Component(string applicationPath, string arguments = null)
178+
public static async Task InvokeWin32Component(string applicationPath, string arguments = null, bool runAsAdmin = false)
178179
{
179-
await InvokeWin32Components(new List<string>() { applicationPath }, arguments);
180+
await InvokeWin32Components(new List<string>() { applicationPath }, arguments, runAsAdmin);
180181
}
181182

182-
public static async Task InvokeWin32Components(List<string> applicationPaths, string arguments = null)
183+
public static async Task InvokeWin32Components(List<string> applicationPaths, string arguments = null, bool runAsAdmin = false)
183184
{
184185
Debug.WriteLine("Launching EXE in FullTrustProcess");
185186
if (App.Connection != null)
186187
{
187188
var value = new ValueSet();
189+
value.Add("Application", applicationPaths.FirstOrDefault());
190+
if (runAsAdmin)
191+
{
192+
value.Add("Arguments", "runas");
193+
}
194+
else
195+
{
196+
value.Add("Arguments", arguments);
197+
}
188198
value.Add("ApplicationList", JsonConvert.SerializeObject(applicationPaths));
189-
value.Add("Arguments", arguments);
190199
await App.Connection.SendMessageAsync(value);
191200
}
192201
}
@@ -264,6 +273,23 @@ public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
264273
return (TEnum)Enum.Parse(typeof(TEnum), text);
265274
}
266275

276+
public async void RunAsAdmin_Click()
277+
{
278+
await InvokeWin32Component(CurrentInstance.ContentPage.SelectedItem.ItemPath, null, true);
279+
}
280+
281+
public async void RunAsAnotherUser_Click()
282+
{
283+
if (CurrentInstance.FilesystemViewModel.WorkingDirectory.StartsWith(AppSettings.RecycleBinPath))
284+
{
285+
// Do not open files and folders inside the recycle bin
286+
return;
287+
}
288+
289+
var clickedOnItem = CurrentInstance.ContentPage.SelectedItem;
290+
await InvokeWin32Component(clickedOnItem.ItemPath, "runasuser", false);
291+
}
292+
267293
public void OpenItem_Click(object sender, RoutedEventArgs e)
268294
{
269295
OpenSelectedItems(false);
@@ -1276,4 +1302,4 @@ public async Task<string> GetHashForFile(ListedItem fileItem, string nameOfAlg,
12761302
return CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset()).ToLower();
12771303
}
12781304
}
1279-
}
1305+
}

Files/MultilingualResources/Files.de-DE.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,14 @@
893893
<source>{0:#,##0} Files, {1:#,##0} Folders</source>
894894
<target state="new">{0:#,##0} Files, {1:#,##0} Folders</target>
895895
</trans-unit>
896+
<trans-unit id="BaseLayoutContextFlyoutRunAsAdmin.Text" translate="yes" xml:space="preserve">
897+
<source>Run as administrator</source>
898+
<target state="new">Run as administrator</target>
899+
</trans-unit>
900+
<trans-unit id="BaseLayoutContextFlyoutRunAsAnotherUser.Text" translate="yes" xml:space="preserve">
901+
<source>Run as another user</source>
902+
<target state="new">Run as another user</target>
903+
</trans-unit>
896904
<trans-unit id="PropertiesDriveItemTypesEquals" translate="yes" xml:space="preserve">
897905
<source>All type of {0}</source>
898906
<target state="new">All type of {0}</target>

Files/MultilingualResources/Files.es-ES.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,14 @@
886886
<source>{0:#,##0} Files, {1:#,##0} Folders</source>
887887
<target state="new">{0:#,##0} Files, {1:#,##0} Folders</target>
888888
</trans-unit>
889+
<trans-unit id="BaseLayoutContextFlyoutRunAsAdmin.Text" translate="yes" xml:space="preserve">
890+
<source>Run as administrator</source>
891+
<target state="new">Run as administrator</target>
892+
</trans-unit>
893+
<trans-unit id="BaseLayoutContextFlyoutRunAsAnotherUser.Text" translate="yes" xml:space="preserve">
894+
<source>Run as another user</source>
895+
<target state="new">Run as another user</target>
896+
</trans-unit>
889897
<trans-unit id="PropertiesDriveItemTypesEquals" translate="yes" xml:space="preserve">
890898
<source>All type of {0}</source>
891899
<target state="new">All type of {0}</target>

Files/MultilingualResources/Files.fr-FR.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,14 @@
889889
<source>{0:#,##0} Files, {1:#,##0} Folders</source>
890890
<target state="new">{0:#,##0} Files, {1:#,##0} Folders</target>
891891
</trans-unit>
892+
<trans-unit id="BaseLayoutContextFlyoutRunAsAdmin.Text" translate="yes" xml:space="preserve">
893+
<source>Run as administrator</source>
894+
<target state="new">Run as administrator</target>
895+
</trans-unit>
896+
<trans-unit id="BaseLayoutContextFlyoutRunAsAnotherUser.Text" translate="yes" xml:space="preserve">
897+
<source>Run as another user</source>
898+
<target state="new">Run as another user</target>
899+
</trans-unit>
892900
<trans-unit id="PropertiesDriveItemTypesEquals" translate="yes" xml:space="preserve">
893901
<source>All type of {0}</source>
894902
<target state="new">All type of {0}</target>

Files/MultilingualResources/Files.it-IT.xlf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,14 @@
894894
<source>{0:#,##0} Files, {1:#,##0} Folders</source>
895895
<target state="new">{0:#,##0} Files, {1:#,##0} Folders</target>
896896
</trans-unit>
897+
<trans-unit id="BaseLayoutContextFlyoutRunAsAdmin.Text" translate="yes" xml:space="preserve">
898+
<source>Run as administrator</source>
899+
<target state="new">Run as administrator</target>
900+
</trans-unit>
901+
<trans-unit id="BaseLayoutContextFlyoutRunAsAnotherUser.Text" translate="yes" xml:space="preserve">
902+
<source>Run as another user</source>
903+
<target state="new">Run as another user</target>
904+
</trans-unit>
897905
<trans-unit id="PropertiesDriveItemTypesEquals" translate="yes" xml:space="preserve">
898906
<source>All type of {0}</source>
899907
<target state="new">All type of {0}</target>

Files/MultilingualResources/Files.ja-JP.xlf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,14 @@
887887
<source>{0:#,##0} Files, {1:#,##0} Folders</source>
888888
<target state="new">{0:#,##0} Files, {1:#,##0} Folders</target>
889889
</trans-unit>
890+
<trans-unit id="BaseLayoutContextFlyoutRunAsAdmin.Text" translate="yes" xml:space="preserve">
891+
<source>Run as administrator</source>
892+
<target state="new">Run as administrator</target>
893+
</trans-unit>
894+
<trans-unit id="BaseLayoutContextFlyoutRunAsAnotherUser.Text" translate="yes" xml:space="preserve">
895+
<source>Run as another user</source>
896+
<target state="new">Run as another user</target>
897+
</trans-unit>
890898
<trans-unit id="PropertiesDriveItemTypesEquals" translate="yes" xml:space="preserve">
891899
<source>All type of {0}</source>
892900
<target state="new">All type of {0}</target>
@@ -918,4 +926,4 @@
918926
</group>
919927
</body>
920928
</file>
921-
</xliff>
929+
</xliff>

0 commit comments

Comments
 (0)