Function to shrink file path to be more human readable

前端 未结 8 788
北海茫月
北海茫月 2020-12-31 20:15

Is there any function in c# to shink a file path ?

Input: \"c:\\users\\Windows\\Downloaded Program Files\\Folder\\Inside\\example\\file.txt\"

<
8条回答
  •  情书的邮戳
    2020-12-31 21:14

    Jeff Atwood posted a solution to this on his blog and here it is :

    [DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
    static extern bool PathCompactPathEx([Out] StringBuilder pszOut, string szPath, int cchMax, int dwFlags);
    
    static string PathShortener(string path, int length)
    {
        StringBuilder sb = new StringBuilder();
        PathCompactPathEx(sb, path, length, 0);
        return sb.ToString();
    }
    

    It uses the unmanaged function PathCompactPathEx to achieve what you want.

提交回复
热议问题