How to

truncate file path

Published: 27. January 2009 | Updated: 26. September 2011
License: Microsoft Public License (MS-PL)
Categories: File system, Windows » Win32
Tags: C# File System Win32 Windows
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at analyzant.com

Import namespaces

using System.Runtime.InteropServices;
using System.Text;

Win API

[DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
static extern bool PathCompactPathEx([Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags);

Use

string path = @"C:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft .NET Framework 3.5 SP1\test.dll";

StringBuilder sb = new StringBuilder();
int length = 40;

// truncate path
PathCompactPathEx(sb, path, length, 0);

Console.WriteLine(sb);
Console Output:
C:\WINDOWS\Microsoft.NET\Fr...\test.dll
Send us feedback about this snippet »



Related Snippets: