#region using System.Runtime.InteropServices; using System.Text; #endregion namespace CSFrameworkV5.Common { /// /// Converts file and directory paths to their respective /// long and short name versions. /// 文件或目录的长文件名与短文件名互转换类 /// /// This class uses InteropServices to call GetLongPathName and GetShortPathName public class ShellPathNameConvertNEW { [DllImport("kernel32.dll")] private static extern uint GetLongPathName(string shortname, StringBuilder longnamebuff, uint buffersize); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName( [MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength); /// /// The ToShortPathNameToLongPathName function retrieves the long path form of a specified short input path /// /// The short name path /// A long name path string public static string ToLongPathName(string shortName) { var longNameBuffer = new StringBuilder(256); var bufferSize = (uint)longNameBuffer.Capacity; GetLongPathName(shortName, longNameBuffer, bufferSize); return longNameBuffer.ToStringEx(); } /// /// The ToLongPathNameToShortPathName function retrieves the short path form of a specified long input path /// /// The long name path /// A short name path string public static string ToShortPathName(string longName) { var shortNameBuffer = new StringBuilder(256); var bufferSize = shortNameBuffer.Capacity; var result = GetShortPathName(longName, shortNameBuffer, bufferSize); return shortNameBuffer.ToStringEx(); } } }