Skip to content

Commit

Permalink
Support short filenames in open path (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
srwi committed Jul 22, 2023
1 parent 2c7bd14 commit 0a86745
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion EverythingToolbar/Helpers/ShellUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
using NLog;

namespace EverythingToolbar.Helpers
{
class ShellUtils
{
private static readonly ILogger Logger = ToolbarLogger.GetLogger<ShellUtils>();

private ShellUtils() { }

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
Expand Down Expand Up @@ -130,12 +134,36 @@ private static bool WindowsExplorerIsDefault()

return true;
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPWStr)]
string path,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder shortPath,
int shortPathLength
);

private static string GetShortPath(string path)
{
var shortPathBuilder = new StringBuilder(255);
var result = GetShortPathName(@"\\?\" + path, shortPathBuilder, shortPathBuilder.Capacity);

if (result == 0)
{
Logger.Info($"Failed to get short path for '{path}'.");
return path;
}

return shortPathBuilder.ToString();
}

public static void OpenParentFolderAndSelect(string path)
{
if (WindowsExplorerIsDefault())
{
CreateProcessFromCommandLine($"explorer.exe /select,\"{path}\"");
var shortPath = GetShortPath(path);
CreateProcessFromCommandLine($"explorer.exe /select,\"{shortPath}\"");
return;
}

Expand Down

0 comments on commit 0a86745

Please sign in to comment.