PathCompactPath (shlwapi)
Last changed: -92.124.113.170

.
Summary
Truncates a file path to fit within a given pixel width by replacing path components with ellipses. The path parameter must be preallocated to MAX_PATH length in order to avoid a buffer overrun / corruption.

C# Signature:

[DllImport("shlwapi.dll", CharSet=CharSet.Auto)]
static extern bool PathCompactPath(IntPtr hDC, [In, Out] StringBuilder pszPath, int dx);

VB Signature:

Declare Function PathCompactPath Lib "shlwapi" (ByVal hDC As IntPtr, <MarshalAs(UnmanagedType.VBByRefStr, sizeConst:=MAX_PATH)> lpszPath As String, ByVal dx As Int32) As Boolean

User-Defined Types:

None.

Notes:

If you do not have a HDC available, take a look at PathCompactPathEx

Tips & Tricks:

Please add some!

Sample Code:

public partial class frmMain : Form

{

    [DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
    static extern bool PathCompactPath(IntPtr hDC, [In, Out] StringBuilder pszPath, int dx);

    public string PathCompactPath(string path, int pixel)
    {
    Graphics gc = this.CreateGraphics();
    StringBuilder strBuffer = new StringBuilder(path);
    PathCompactPath((IntPtr)gc.GetHdc(), strBuffer, pixel);
    return strBuffer.ToString();
    }

}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation