Skip to content

Commit 0f5b02f

Browse files
Open file with associated program
Prevent opening zip in Explorer Fix working directory issue
1 parent a220284 commit 0f5b02f

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,36 @@ public static void OpenPath(string fileOrFolderPath)
170170
}
171171
}
172172

173+
/// <summary>
174+
/// Open a file with associated application
175+
/// </summary>
176+
/// <param name="filePath">File path</param>
177+
/// <param name="workingDir">Working directory</param>
178+
/// <param name="asAdmin">Open as Administrator</param>
179+
public static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false)
180+
{
181+
var psi = new ProcessStartInfo
182+
{
183+
FileName = filePath,
184+
UseShellExecute = true,
185+
WorkingDirectory = workingDir,
186+
Verb = asAdmin ? "runas" : string.Empty
187+
};
188+
try
189+
{
190+
if (FileExists(filePath))
191+
Process.Start(psi);
192+
}
193+
catch (Exception)
194+
{
195+
#if DEBUG
196+
throw;
197+
#else
198+
MessageBox.Show(string.Format("Unable to open the path {0}, please check if it exists", filePath));
199+
#endif
200+
}
201+
}
202+
173203
///<summary>
174204
/// This checks whether a given string is a directory path or network location string.
175205
/// It does not check if location actually exists.

Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ internal static Result CreateFolderResult(string title, string subtitle, string
123123
}
124124
else
125125
{
126-
// or make this folder the current query
127-
Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword));
126+
// or make this folder the current query
127+
Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder, query.ActionKeyword));
128128
}
129129

130130
return false;
@@ -255,15 +255,15 @@ internal static Result CreateFileResult(string filePath, Query query, int score
255255
{
256256
if (c.SpecialKeyState.ToModifierKeys() == (ModifierKeys.Control | ModifierKeys.Shift))
257257
{
258-
OpenFileAsAdmin(filePath);
258+
OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty, true);
259259
}
260260
else if (c.SpecialKeyState.ToModifierKeys() == ModifierKeys.Control)
261261
{
262262
OpenFolder(filePath, filePath);
263263
}
264264
else
265265
{
266-
OpenFile(filePath);
266+
OpenFile(filePath, Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty);
267267
}
268268
}
269269
catch (Exception ex)
@@ -287,10 +287,10 @@ private static bool IsMedia(string extension)
287287
return MediaExtensions.Contains(extension.ToLowerInvariant());
288288
}
289289

290-
private static void OpenFile(string filePath)
290+
private static void OpenFile(string filePath, string workingDir = "", bool asAdmin = false)
291291
{
292292
IncrementEverythingRunCounterIfNeeded(filePath);
293-
FilesFolders.OpenPath(filePath);
293+
FilesFolders.OpenFile(filePath, workingDir, asAdmin);
294294
}
295295

296296
private static void OpenFolder(string folderPath, string fileNameOrFilePath = null)
@@ -299,28 +299,6 @@ private static void OpenFolder(string folderPath, string fileNameOrFilePath = nu
299299
Context.API.OpenDirectory(folderPath, fileNameOrFilePath);
300300
}
301301

302-
private static void OpenFileAsAdmin(string filePath)
303-
{
304-
_ = Task.Run(() =>
305-
{
306-
try
307-
{
308-
IncrementEverythingRunCounterIfNeeded(filePath);
309-
Process.Start(new ProcessStartInfo
310-
{
311-
FileName = filePath,
312-
UseShellExecute = true,
313-
WorkingDirectory = Settings.UseLocationAsWorkingDir ? Path.GetDirectoryName(filePath) : string.Empty,
314-
Verb = "runas",
315-
});
316-
}
317-
catch (Exception e)
318-
{
319-
MessageBox.Show(e.Message, "Could not start " + filePath);
320-
}
321-
});
322-
}
323-
324302
private static void IncrementEverythingRunCounterIfNeeded(string fileOrFolder)
325303
{
326304
if (Settings.EverythingEnabled)

0 commit comments

Comments
 (0)