mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[PT Run] Open folder using shell instead of explorer.exe (#7292)
* pt run not using explorer.exe for opening path (#4622) * updated explorer action name (#4622)
This commit is contained in:
committed by
GitHub
parent
36dd29c056
commit
b80578b1b9
@@ -0,0 +1,72 @@
|
||||
// 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.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Microsoft.Plugin.Folder.Sources
|
||||
{
|
||||
public class ShellAction : IShellAction
|
||||
{
|
||||
public bool Execute(string path, IPublicAPI contextApi)
|
||||
{
|
||||
if (contextApi == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(contextApi));
|
||||
}
|
||||
|
||||
return OpenFileOrFolder(path, contextApi);
|
||||
}
|
||||
|
||||
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")]
|
||||
private static bool OpenFileOrFolder(string path, IPublicAPI contextApi)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var process = new Process())
|
||||
{
|
||||
process.StartInfo.FileName = path;
|
||||
process.StartInfo.UseShellExecute = true;
|
||||
process.Start();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
string messageBoxTitle = string.Format(CultureInfo.InvariantCulture, "{0} {1}", Properties.Resources.wox_plugin_folder_select_folder_OpenFileOrFolder_error_message, path);
|
||||
Log.Exception($"Failed to open {path}, {e.Message}", e, MethodBase.GetCurrentMethod().DeclaringType);
|
||||
contextApi.ShowMsg(messageBoxTitle, e.Message);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user