From 150b2876c1eaeafa57cf3ebd805666787e20fab6 Mon Sep 17 00:00:00 2001 From: dahall Date: Sat, 22 Feb 2020 15:45:34 -0700 Subject: [PATCH] Added GetShortPath method --- System/Path.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/System/Path.cs b/System/Path.cs index 6e2eecee..032b0b13 100644 --- a/System/Path.cs +++ b/System/Path.cs @@ -176,6 +176,19 @@ namespace Vanara.IO /// public static string GetRootFromDriveNumber(int drive) => SBAllocCallRet((s, sz) => PathBuildRoot(s, drive), "", 4); + /// Gets the short path form of the specified path. + /// The path to convert. + /// The short form of . + 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; + } /// /// Determines if a path string is a valid Universal Naming Convention (UNC) path and extract the name of the server from the path.