1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#region
 
using System.Runtime.InteropServices;
using System.Text;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    /**/
    /// <summary>
    ///     Converts file and directory paths to their respective
    ///     long and short name versions.
    /// </summary>
    /// <remarks>This class uses InteropServices to call GetLongPathName and GetShortPathName</remarks>
    public class ShellPathNameConvert
    {
        [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);
 
        /**/
        /// <summary>
        ///     The ToShortPathNameToLongPathName function retrieves the long path form of a specified short input path
        /// </summary>
        /// <param name="shortName">The short name path</param>
        /// <returns>A long name path string</returns>
        public static string ToLongPathName(string shortName)
        {
            var longNameBuffer = new StringBuilder(256);
            var bufferSize = (uint)longNameBuffer.Capacity;
 
            GetLongPathName(shortName, longNameBuffer, bufferSize);
 
            return longNameBuffer.ToStringEx();
        }
 
        /**/
        /// <summary>
        ///     The ToLongPathNameToShortPathName function retrieves the short path form of a specified long input path
        /// </summary>
        /// <param name="longName">The long name path</param>
        /// <returns>A short name path string</returns>
        public static string ToShortPathName(string longName)
        {
            var shortNameBuffer = new StringBuilder(256);
            var bufferSize = shortNameBuffer.Capacity;
 
            var result =
                GetShortPathName(longName, shortNameBuffer, bufferSize);
 
            return shortNameBuffer.ToStringEx();
        }
    }
}