Skip to content

Commit 7c3511d

Browse files
Added support for opening applications from the pathbox (#7859)
1 parent 81bd6a4 commit 7c3511d

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

src/Files.Launcher/MessageHandlers/ApplicationLaunchHandler.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading.Tasks;
1313
using Vanara.PInvoke;
1414
using Vanara.Windows.Shell;
15+
using Windows.Foundation.Collections;
1516

1617
namespace FilesFullTrust.MessageHandlers
1718
{
@@ -21,7 +22,7 @@ public void Initialize(PipeStream connection)
2122
{
2223
}
2324

24-
public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object> message, string arguments)
25+
public async Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object> message, string arguments)
2526
{
2627
switch (arguments)
2728
{
@@ -38,12 +39,11 @@ public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object
3839
if (message.ContainsKey("Application"))
3940
{
4041
var application = (string)message["Application"];
41-
HandleApplicationLaunch(application, message);
42-
}
43-
else if (message.ContainsKey("ApplicationList"))
44-
{
45-
var applicationList = JsonConvert.DeserializeObject<IEnumerable<string>>((string)message["ApplicationList"]);
46-
HandleApplicationsLaunch(applicationList, message);
42+
var success = await HandleApplicationLaunch(application, message);
43+
await Win32API.SendMessageAsync(connection, new ValueSet()
44+
{
45+
{ "Success", success }
46+
}, message.Get("RequestID", (string)null));
4747
}
4848
break;
4949

@@ -53,22 +53,13 @@ public Task ParseArgumentsAsync(PipeStream connection, Dictionary<string, object
5353
var afPath = Path.Combine(Path.GetTempPath(), "CompatibilityTroubleshooterAnswerFile.xml");
5454
File.WriteAllText(afPath, string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Answers Version=\"1.0\"><Interaction ID=\"IT_LaunchMethod\"><Value>CompatTab</Value></Interaction><Interaction ID=\"IT_BrowseForFile\"><Value>{0}</Value></Interaction></Answers>", filePath));
5555
message["Parameters"] = $"/id PCWDiagnostic /af \"{afPath}\"";
56-
HandleApplicationLaunch("msdt.exe", message);
56+
await HandleApplicationLaunch("msdt.exe", message);
5757
}
5858
break;
5959
}
60-
return Task.CompletedTask;
61-
}
62-
63-
private void HandleApplicationsLaunch(IEnumerable<string> applications, Dictionary<string, object> message)
64-
{
65-
foreach (var application in applications)
66-
{
67-
HandleApplicationLaunch(application, message);
68-
}
6960
}
7061

71-
private async void HandleApplicationLaunch(string application, Dictionary<string, object> message)
62+
private async Task<bool> HandleApplicationLaunch(string application, Dictionary<string, object> message)
7263
{
7364
var arguments = message.Get("Parameters", "");
7465
var workingDirectory = message.Get("WorkingDirectory", "");
@@ -77,8 +68,7 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
7768
if (new[] { ".vhd", ".vhdx" }.Contains(Path.GetExtension(application).ToLowerInvariant()))
7869
{
7970
// Use powershell to mount vhds as this requires admin rights
80-
Win32API.MountVhdDisk(application);
81-
return;
71+
return Win32API.MountVhdDisk(application);
8272
}
8373

8474
try
@@ -123,6 +113,7 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
123113
process.StartInfo.WorkingDirectory = workingDirectory;
124114
process.Start();
125115
Win32API.BringToForeground(currentWindows);
116+
return true;
126117
}
127118
catch (Win32Exception)
128119
{
@@ -136,12 +127,13 @@ private async void HandleApplicationLaunch(string application, Dictionary<string
136127
{
137128
process.Start();
138129
Win32API.BringToForeground(currentWindows);
130+
return true;
139131
}
140132
catch (Win32Exception)
141133
{
142134
try
143135
{
144-
await Win32API.StartSTATask(() =>
136+
return await Win32API.StartSTATask(() =>
145137
{
146138
var split = application.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => GetMtpPath(x));
147139
if (split.Count() == 1)
@@ -172,16 +164,19 @@ await Win32API.StartSTATask(() =>
172164
catch (Win32Exception)
173165
{
174166
// Cannot open file (e.g DLL)
167+
return false;
175168
}
176169
catch (ArgumentException)
177170
{
178171
// Cannot open file (e.g DLL)
172+
return false;
179173
}
180174
}
181175
}
182176
catch (InvalidOperationException)
183177
{
184178
// Invalid file path
179+
return false;
185180
}
186181
}
187182

src/Files.Launcher/Win32API.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ public static void SetVolumeLabel(string driveName, string newLabel)
354354
RunPowershellCommand($"-command \"$Signature = '[DllImport(\\\"kernel32.dll\\\", SetLastError = false)]public static extern bool SetVolumeLabel(string lpRootPathName, string lpVolumeName);'; $SetVolumeLabel = Add-Type -MemberDefinition $Signature -Name \"Win32SetVolumeLabel\" -Namespace Win32Functions -PassThru; $SetVolumeLabel::SetVolumeLabel('{driveName}', '{newLabel}')\"", true);
355355
}
356356

357-
public static void MountVhdDisk(string vhdPath)
357+
public static bool MountVhdDisk(string vhdPath)
358358
{
359359
// mounting requires elevation
360-
RunPowershellCommand($"-command \"Mount-DiskImage -ImagePath '{vhdPath}'\"", true);
360+
return RunPowershellCommand($"-command \"Mount-DiskImage -ImagePath '{vhdPath}'\"", true);
361361
}
362362

363363
public static Bitmap GetBitmapFromHBitmap(HBITMAP hBitmap)

src/Files/Helpers/Win32Helpers.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public static async Task<bool> InvokeWin32ComponentsAsync(IEnumerable<string> ap
3232
{
3333
{ "Arguments", "LaunchApp" },
3434
{ "WorkingDirectory", string.IsNullOrEmpty(workingDirectory) ? associatedInstance?.FilesystemViewModel?.WorkingDirectory : workingDirectory },
35-
{ "Application", applicationPaths.FirstOrDefault() },
36-
{ "ApplicationList", JsonConvert.SerializeObject(applicationPaths) },
35+
{ "Application", applicationPaths.FirstOrDefault() }
3736
};
3837

3938
if (runAsAdmin)

src/Files/ViewModels/NavToolbarViewModel.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System;
1515
using System.Collections.Generic;
1616
using System.Collections.ObjectModel;
17+
using System.Diagnostics;
1718
using System.IO;
1819
using System.Linq;
1920
using System.Threading.Tasks;
@@ -955,6 +956,11 @@ public async Task CheckPathInput(string currentInput, string currentSelectedPath
955956
}
956957
}
957958

959+
if (await LaunchApplicationFromPath(currentInput, workingDir))
960+
{
961+
return;
962+
}
963+
958964
try
959965
{
960966
if (!await Launcher.LaunchUriAsync(new Uri(currentInput)))
@@ -976,6 +982,35 @@ await DialogDisplayHelper.ShowDialogAsync("InvalidItemDialogTitle".GetLocalized(
976982
}
977983
}
978984

985+
private static async Task<bool> LaunchApplicationFromPath(string currentInput, string workingDir)
986+
{
987+
var trimmedInput= currentInput.Trim();
988+
var fileName = trimmedInput;
989+
var arguments = "";
990+
if (trimmedInput.Contains(' '))
991+
{
992+
var positionOfBlank = trimmedInput.IndexOf(' ');
993+
fileName = trimmedInput.Substring(0, positionOfBlank);
994+
arguments = currentInput.Substring(currentInput.IndexOf(' '));
995+
}
996+
997+
var connection = await AppServiceConnectionHelper.Instance;
998+
if (connection != null)
999+
{
1000+
var value = new ValueSet()
1001+
{
1002+
{ "Arguments", "LaunchApp" },
1003+
{ "WorkingDirectory", workingDir },
1004+
{ "Application", fileName },
1005+
{ "Parameters", arguments }
1006+
};
1007+
var (status, response) = await connection.SendMessageForResponseAsync(value);
1008+
return status == Windows.ApplicationModel.AppService.AppServiceResponseStatus.Success && response.Get("Success", false);
1009+
}
1010+
1011+
return false;
1012+
}
1013+
9791014
public async void SetAddressBarSuggestions(AutoSuggestBox sender, IShellPage shellpage, int maxSuggestions = 7)
9801015
{
9811016
if (!string.IsNullOrWhiteSpace(sender.Text) && shellpage.FilesystemViewModel != null)

0 commit comments

Comments
 (0)