Open file with associated program

Prevent opening zip in Explorer
Fix working directory issue
This commit is contained in:
Vic 2023-03-24 15:22:29 +08:00
parent a22028460d
commit 0f5b02fa38
2 changed files with 36 additions and 28 deletions

View file

@ -170,6 +170,36 @@ namespace Flow.Launcher.Plugin.SharedCommands
} }
} }
/// <summary>
/// Open a file with associated application
/// </summary>
/// <param name="filePath">File path</param>
/// <param name="workingDir">Working directory</param>
/// <param name="asAdmin">Open as Administrator</param>
public static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false)
{
var psi = new ProcessStartInfo
{
FileName = filePath,
UseShellExecute = true,
WorkingDirectory = workingDir,
Verb = asAdmin ? "runas" : string.Empty
};
try
{
if (FileExists(filePath))
Process.Start(psi);
}
catch (Exception)
{
#if DEBUG
throw;
#else
MessageBox.Show(string.Format("Unable to open the path {0}, please check if it exists", filePath));
#endif
}
}
///<summary> ///<summary>
/// This checks whether a given string is a directory path or network location string. /// This checks whether a given string is a directory path or network location string.
/// It does not check if location actually exists. /// It does not check if location actually exists.

View file

@ -255,7 +255,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
{ {
if (c.SpecialKeyState.ToModifierKeys() == (ModifierKeys.Control | ModifierKeys.Shift)) if (c.SpecialKeyState.ToModifierKeys() == (ModifierKeys.Control | ModifierKeys.Shift))
{ {
OpenFileAsAdmin(filePath); OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, true);
} }
else if (c.SpecialKeyState.ToModifierKeys() == ModifierKeys.Control) else if (c.SpecialKeyState.ToModifierKeys() == ModifierKeys.Control)
{ {
@ -263,7 +263,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
} }
else else
{ {
OpenFile(filePath); OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -287,10 +287,10 @@ namespace Flow.Launcher.Plugin.Explorer.Search
return MediaExtensions.Contains(extension.ToLowerInvariant()); return MediaExtensions.Contains(extension.ToLowerInvariant());
} }
private static void OpenFile(string filePath) private static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false)
{ {
IncrementEverythingRunCounterIfNeeded(filePath); IncrementEverythingRunCounterIfNeeded(filePath);
FilesFolders.OpenPath(filePath); FilesFolders.OpenFile(filePath, workingDir, asAdmin);
} }
private static void OpenFolder(string folderPath, string fileNameOrFilePath = null) private static void OpenFolder(string folderPath, string fileNameOrFilePath = null)
@ -299,28 +299,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search
Context.API.OpenDirectory(folderPath, fileNameOrFilePath); Context.API.OpenDirectory(folderPath, fileNameOrFilePath);
} }
private static void OpenFileAsAdmin(string filePath)
{
_ = Task.Run(() =>
{
try
{
IncrementEverythingRunCounterIfNeeded(filePath);
Process.Start(new ProcessStartInfo
{
FileName = filePath,
UseShellExecute = true,
WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty,
Verb = "runas",
});
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Could not start " + filePath);
}
});
}
private static void IncrementEverythingRunCounterIfNeeded(string fileOrFolder) private static void IncrementEverythingRunCounterIfNeeded(string fileOrFolder)
{ {
if (Settings.EverythingEnabled) if (Settings.EverythingEnabled)