From 7024987d238c4d3682fd32110c2ffd60ea262659 Mon Sep 17 00:00:00 2001 From: morkt Date: Wed, 5 Apr 2017 18:38:23 +0400 Subject: [PATCH] (File.CompactPath): new static method. --- GUI/Shell.cs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/GUI/Shell.cs b/GUI/Shell.cs index 0134b362..3b6a233a 100644 --- a/GUI/Shell.cs +++ b/GUI/Shell.cs @@ -31,10 +31,7 @@ using System.Runtime.InteropServices; namespace GARbro.Shell { - /// - /// Wrapper around MoveFileEx WINAPI call. - /// - class File + static class File { [DllImport("kernel32.dll", EntryPoint="MoveFileExW", SetLastError=true, CharSet=CharSet.Unicode)] public static extern bool MoveFileEx (string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags); @@ -50,6 +47,9 @@ namespace GARbro.Shell FailIfNotTrackable = 0x00000020 } + /// + /// Wrapper around MoveFileEx WINAPI call. + /// public static bool Rename (string szFrom, string szTo) { return MoveFileEx (szFrom, szTo, MoveFileFlags.ReplaceExisting); @@ -197,6 +197,21 @@ namespace GARbro.Shell return Delete (path, FileOperationFlags.FOF_NOCONFIRMATION | FileOperationFlags.FOF_NOERRORUI | FileOperationFlags.FOF_SILENT); } + + [DllImport("shlwapi.dll", EntryPoint="PathCompactPathExW", CharSet = CharSet.Unicode)] + static extern bool PathCompactPathEx ([Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags); + + const int MAX_PATH = 0x104; + + /// + /// Truncates a path to fit within a certain number of characters by replacing path components with ellipses. + /// + public static string CompactPath (string name, int length) + { + var sb = new StringBuilder (MAX_PATH); + PathCompactPathEx (sb, name, Math.Min (length+1, MAX_PATH), 0); + return sb.ToString(); + } } public class TemporaryFile : IDisposable