c# 获取网络驱动器路径

 1  public static class Pathing
 2     {
 3         [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 4         public static extern int WNetGetConnection(
 5             [MarshalAs(UnmanagedType.LPTStr)] string localName,
 6             [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName,
 7             ref int length);
 8 
 9         public static string GetUNCPath(string originalPath)  
10         {  
11             StringBuilder sb = new StringBuilder(512);  
12             int size = sb.Capacity;  
13             if (originalPath.Length > 2 && originalPath[1] == ':')  
14             {  
15                 char c = originalPath[0];  
16                 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))  
17                 {  
18                     int error = WNetGetConnection(originalPath.Substring(0, 2),  
19                         sb, ref size);  
20                     if (error == 0)  
21                     {  
22                         DirectoryInfo dir = new DirectoryInfo(originalPath);  
23                         string path = Path.GetFullPath(originalPath)  
24                             .Substring(Path.GetPathRoot(originalPath).Length);  
25                         return Path.Combine(sb.ToString().TrimEnd(), path);  
26                     }  
27                 }  
28             }  
29             return originalPath;  
30         }  
31     }
1 string path=@“z:\”;
2 Pathing.GetUNCPath(path)
posted @ 2012-10-24 13:04  怪獣  阅读(1468)  评论(3编辑  收藏