ShellAction.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.Globalization; 7 using System.Text.RegularExpressions; 8 9 using Wox.Infrastructure; 10 using Wox.Plugin; 11 12 namespace Microsoft.Plugin.Folder.Sources 13 { 14 public class ShellAction : IShellAction 15 { 16 public bool Execute(string sanitizedPath, IPublicAPI contextApi) 17 { 18 ArgumentNullException.ThrowIfNull(contextApi); 19 20 return OpenFileOrFolder(sanitizedPath, contextApi); 21 } 22 23 public bool ExecuteSanitized(string search, IPublicAPI contextApi) 24 { 25 ArgumentNullException.ThrowIfNull(contextApi); 26 27 return Execute(SanitizedPath(search), contextApi); 28 } 29 30 private static string SanitizedPath(string search) 31 { 32 var sanitizedPath = Regex.Replace(search, @"[\/\\]+", "\\"); 33 34 // A network path must start with \\ 35 // Using Ordinal since this is internal and used with a symbol 36 if (!sanitizedPath.StartsWith('\\')) 37 { 38 return sanitizedPath; 39 } 40 41 return sanitizedPath.Insert(0, "\\"); 42 } 43 44 private static bool OpenFileOrFolder(string path, IPublicAPI contextApi) 45 { 46 if (!Helper.OpenInShell(path)) 47 { 48 var message = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", Properties.Resources.wox_plugin_folder_select_folder_OpenFileOrFolder_error_message, path); 49 contextApi.ShowMsg(message); 50 } 51 52 return true; 53 } 54 } 55 }