2020-10-01 05:37:46 +02:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
using System.Diagnostics ;
using System.Globalization ;
using System.Reflection ;
using System.Text.RegularExpressions ;
using Wox.Plugin ;
2020-10-23 13:06:22 -07:00
using Wox.Plugin.Logger ;
2020-10-01 05:37:46 +02:00
namespace Microsoft.Plugin.Folder.Sources
{
2020-10-17 01:30:11 +02:00
public class ShellAction : IShellAction
2020-10-01 05:37:46 +02:00
{
public bool Execute ( string path , IPublicAPI contextApi )
{
if ( contextApi = = null )
{
throw new ArgumentNullException ( nameof ( contextApi ) ) ;
}
2020-10-17 01:30:11 +02:00
return OpenFileOrFolder ( path , contextApi ) ;
2020-10-01 05:37:46 +02:00
}
public bool ExecuteSanitized ( string search , IPublicAPI contextApi )
{
if ( contextApi = = null )
{
throw new ArgumentNullException ( nameof ( contextApi ) ) ;
}
return Execute ( SanitizedPath ( search ) , contextApi ) ;
}
private static string SanitizedPath ( string search )
{
var sanitizedPath = Regex . Replace ( search , @"[\/\\]+" , "\\" ) ;
// A network path must start with \\
if ( ! sanitizedPath . StartsWith ( "\\" , StringComparison . InvariantCulture ) )
{
return sanitizedPath ;
}
return sanitizedPath . Insert ( 0 , "\\" ) ;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive and instead inform the user of the error")]
2020-10-17 01:30:11 +02:00
private static bool OpenFileOrFolder ( string path , IPublicAPI contextApi )
2020-10-01 05:37:46 +02:00
{
try
{
2020-10-17 01:30:11 +02:00
using ( var process = new Process ( ) )
{
process . StartInfo . FileName = path ;
process . StartInfo . UseShellExecute = true ;
process . Start ( ) ;
}
2020-10-01 05:37:46 +02:00
}
catch ( Exception e )
{
string messageBoxTitle = string . Format ( CultureInfo . InvariantCulture , "{0} {1}" , Properties . Resources . wox_plugin_folder_select_folder_OpenFileOrFolder_error_message , path ) ;
2020-10-17 01:30:11 +02:00
Log . Exception ( $"Failed to open {path}, {e.Message}" , e , MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
2020-10-01 05:37:46 +02:00
contextApi . ShowMsg ( messageBoxTitle , e . Message ) ;
}
return true ;
}
}
}