Added GetShortPath method

pull/119/head
dahall 2020-02-22 15:45:34 -07:00
parent 4eea954b4d
commit 150b2876c1
1 changed files with 13 additions and 0 deletions

View File

@ -176,6 +176,19 @@ namespace Vanara.IO
/// </returns>
public static string GetRootFromDriveNumber(int drive) => SBAllocCallRet((s, sz) => PathBuildRoot(s, drive), "", 4);
/// <summary>Gets the short path form of the specified path.</summary>
/// <param name="path">The path to convert.</param>
/// <returns>The short form of <paramref name="path"/>.</returns>
public static string GetShortPath(string path)
{
if (string.IsNullOrEmpty(path)) return path;
var shortPath = path;
var l = path.Length + 5;
var sb = new StringBuilder(l, l);
var rl = GetShortPathName(path.Length > MAX_PATH ? @"\\?\" + path : path, sb, (uint)sb.Capacity);
if (rl > 0 && rl <= l) shortPath = sb.ToString();
return shortPath;
}
/// <summary>
/// Determines if a path string is a valid Universal Naming Convention (UNC) path and extract the name of the server from the path.