/ src / modules / cmdpal / ext / Microsoft.CmdPal.Ext.WindowsSettings / Commands / OpenSettingsCommand.cs
OpenSettingsCommand.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.Generic; 7 using System.Diagnostics; 8 using System.Linq; 9 using System.Resources; 10 using System.Text; 11 using System.Threading.Tasks; 12 using Microsoft.CmdPal.Ext.WindowsSettings.Classes; 13 using Microsoft.CmdPal.Ext.WindowsSettings.Properties; 14 using Microsoft.CommandPalette.Extensions; 15 using Microsoft.CommandPalette.Extensions.Toolkit; 16 using Windows.ApplicationModel.DataTransfer; 17 using Windows.Networking.NetworkOperators; 18 using Windows.UI; 19 20 namespace Microsoft.CmdPal.Ext.WindowsSettings.Commands; 21 22 internal sealed partial class OpenSettingsCommand : InvokableCommand 23 { 24 private readonly WindowsSetting _entry; 25 26 internal OpenSettingsCommand(WindowsSetting entry) 27 { 28 Name = Resources.OpenSettings; 29 Icon = Icons.CopyIcon; 30 _entry = entry; 31 } 32 33 private static bool DoOpenSettingsAction(WindowsSetting entry) 34 { 35 ProcessStartInfo processStartInfo; 36 37 var command = entry.Command; 38 39 if (command.Contains("%windir%", StringComparison.InvariantCultureIgnoreCase)) 40 { 41 var windowsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Windows); 42 command = command.Replace("%windir%", windowsFolder, StringComparison.InvariantCultureIgnoreCase); 43 } 44 45 if (command.Contains(' ')) 46 { 47 var commandSplit = command.Split(' '); 48 var file = commandSplit.FirstOrDefault() ?? string.Empty; 49 var arguments = command[file.Length..].TrimStart(); 50 51 processStartInfo = new ProcessStartInfo(file, arguments) 52 { 53 UseShellExecute = false, 54 }; 55 } 56 else 57 { 58 processStartInfo = new ProcessStartInfo(command) 59 { 60 UseShellExecute = true, 61 }; 62 } 63 64 try 65 { 66 Process.Start(processStartInfo); 67 return true; 68 } 69 #pragma warning disable CS0168, IDE0059 70 catch (Exception exception) 71 { 72 // TODO GH #108 Logging is something we have to take care of 73 // Log.Exception("can't open settings", exception, typeof(ResultHelper)); 74 return false; 75 } 76 #pragma warning restore CS0168, IDE0059 77 } 78 79 public override CommandResult Invoke() 80 { 81 DoOpenSettingsAction(_entry); 82 83 return CommandResult.Dismiss(); 84 } 85 }