在windows机制下,路径长度最多255个,下面利用系统API函数将长路径转换成短路径

1.函数定义

View Code
1 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
2         static extern uint GetShortPathName
3         (
4             [MarshalAs(UnmanagedType.LPTStr)]
5             string lpszLongPath,
6             [MarshalAs(UnmanagedType.LPTStr)]
7             StringBuilder lpszShortPath,
8             uint cchBuffer
9         );

2.函数调用

View Code
 1         /// <summary>
 2         /// 作用:将长路径转换短路径
 3         /// </summary>
 4         /// <param name="longName">长路径</param>
 5         /// <returns></returns>
 6         public static string ToShortPathName(string longName)
 7         {
 8             try
 9             {
10                 uint bufferSize = 512;
11                 StringBuilder shortNameBuffer = new StringBuilder((int)bufferSize);
12                 uint result = GetShortPathName(longName, shortNameBuffer, bufferSize);
13                 return shortNameBuffer.ToString();
14             }
15             catch
16             {
17                 
18             }
19         }
posted on 2012-05-11 13:32  捣乃忒  阅读(488)  评论(0编辑  收藏  举报