Skip to content

Commit

Permalink
Fix handling of third party file explorers (#373, #338)
Browse files Browse the repository at this point in the history
Closes #373
  • Loading branch information
srwi committed Apr 19, 2023
1 parent 7b90027 commit 1b172c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
15 changes: 4 additions & 11 deletions EverythingToolbar/Data/SearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,10 @@ public void Open()
{
try
{
if (IsFile)
{
Process.Start(new ProcessStartInfo(FullPathAndFileName)
{
WorkingDirectory = Path
});
}
else
Process.Start(new ProcessStartInfo(FullPathAndFileName)
{
ShellUtils.OpenFolderWithDefaultApp(FullPathAndFileName);
}
WorkingDirectory = Path
});
EverythingSearch.IncrementRunCount(FullPathAndFileName);
}
catch (Exception e)
Expand Down Expand Up @@ -107,7 +100,7 @@ public void OpenPath()
{
try
{
ShellUtils.OpenParentFolderWithDefaultApp(FullPathAndFileName);
ShellUtils.OpenParentFolderAndSelect(FullPathAndFileName);
EverythingSearch.IncrementRunCount(FullPathAndFileName);
}
catch (Exception e)
Expand Down
39 changes: 17 additions & 22 deletions EverythingToolbar/Helpers/ShellUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,34 +118,29 @@ public static void OpenWithDialog(string path)
Process.Start("rundll32.exe", args);
}

public static void OpenFolderWithDefaultApp(string folder)
private static bool WindowsExplorerIsDefault()
{
if (TryOpenDirectoryWithDefaultApp(folder))
return;
var folderShell = (string)Registry.ClassesRoot.OpenSubKey(@"Folder\shell")?.GetValue(null);
if (folderShell != null && Registry.ClassesRoot.OpenSubKey(@"Folder\shell\" + folderShell + @"\command")?.GetValue(null) != null)
return false;

var directoryShell = (string)Registry.ClassesRoot.OpenSubKey(@"Directory\shell")?.GetValue(null);
if (directoryShell != null && Registry.ClassesRoot.OpenSubKey(@"Directory\shell\" + directoryShell + @"\command")?.GetValue(null) != null)
return false;

CreateProcessFromCommandLine($"explorer.exe \"{folder}\"");
return true;
}

public static void OpenParentFolderWithDefaultApp(string path)
public static void OpenParentFolderAndSelect(string path)
{
if (TryOpenDirectoryWithDefaultApp(Path.GetDirectoryName(path)))
if (WindowsExplorerIsDefault())
{
CreateProcessFromCommandLine($"explorer.exe /select,\"{path}\"");
return;
}

CreateProcessFromCommandLine($"explorer.exe /select,\"{path}\"");
}

private static bool TryOpenDirectoryWithDefaultApp(string directory)
{
var shell = (string)Registry.ClassesRoot.OpenSubKey(@"Directory\shell")?.GetValue("");

var command = Registry.ClassesRoot.OpenSubKey(@"Directory\shell\" + shell + @"\command")?.GetValue("");
if (command == null)
command = Registry.ClassesRoot.OpenSubKey(@"Folder\shell\open\command")?.GetValue("");
if (((string)command).ToLower().Contains(@"\explorer.exe"))
return false;

CreateProcessFromCommandLine(command.ToString().Replace("%1", directory));
return true;
var parent = Path.GetDirectoryName(path) ?? path;
Process.Start(parent);
}
}
}

0 comments on commit 1b172c5

Please sign in to comment.