Tag Archives: short path
How to get the short path of a directory
In some occasions it might be necessary to use a directory’s short path. This is possible with the use of the Windows GetShortPathName
function.
Implementation:
1 2 3 4 5 6 7 8 9 | [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer); private static string GetShortPath(string longPath) { StringBuilder shortPath = new StringBuilder(255); GetShortPathName(longPath, shortPath, 255); return shortPath.ToString(); } |
Simply call the GetShortPath
with the directory you want to get the short path as an argument.
Example:
1 | string applicationShortPath = GetShortPath(Application.ExecutablePath); |
Posted in C#.
Tagged C#, csharp, GetShortPathName, short path, snippet, winforms