C# - Truncate File Path

By [)ia6l0 iii

You can truncate the file paths using the Shell LightWeight library.

Use this code sample.

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

static string TruncatePath( string fullFilePath, int length )
    StringBuilder sb = new StringBuilder();
    PathCompactPathEx( sb, fullFilePath, length, 0 );
    return sb.ToString();

Related FAQs

You can store files as embedded resources and read them from your code at runtime.
Extension method to check if a string array contains a particular string
The IndexOf method in C# is case -sensitive. However, you can use the CompareInto class to make it insensitive to case.
Extension method to check if a substring exists in a string using Regular Expressions
You can read a text file into a string. But practically speaking, not sure why one would do this.
C# - Truncate File Path  (1928 Views)