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>
/// This checks whether a given string is a directory path or network location string.
/// It does not check if location actually exists.

View file

@ -123,8 +123,8 @@ namespace Flow.Launcher.Plugin.Explorer.Search
}
else
{
// or make this folder the current query
Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword));
// or make this folder the current query
Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword));
}
return false;
@ -255,7 +255,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
{
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)
{
@ -263,7 +263,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
}
else
{
OpenFile(filePath);
OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty);
}
}
catch (Exception ex)
@ -287,10 +287,10 @@ namespace Flow.Launcher.Plugin.Explorer.Search
return MediaExtensions.Contains(extension.ToLowerInvariant());
}
private static void OpenFile(string filePath)
private static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false)
{
IncrementEverythingRunCounterIfNeeded(filePath);
FilesFolders.OpenPath(filePath);
FilesFolders.OpenFile(filePath, workingDir, asAdmin);
}
private static void OpenFolder(string folderPath, string fileNameOrFilePath = null)
@ -299,28 +299,6 @@ namespace Flow.Launcher.Plugin.Explorer.Search
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)
{
if (Settings.EverythingEnabled)