{"id":22,"date":"2008-03-02T09:13:20","date_gmt":"2008-03-02T15:13:20","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/post.aspx?id=c7dc047f-e6c7-45e2-9303-532c8d6ad48c"},"modified":"2008-09-17T20:43:28","modified_gmt":"2008-09-18T01:43:28","slug":"converting-a-mapped-drive-letter-to-a-network-path-using-c","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/22","title":{"rendered":"Converting a mapped drive letter to a network path using C#"},"content":{"rendered":"
Occasionally you might have the need to convert a mapped drive letter to a UNC or network path. For example, a drive letter such as “Z” might be mapped to a network share:<\/p>\n
<\/a><\/p>\n In this example, the “Z” drive is mapped to a “Personal” folder on a server named “Home”.<\/p>\n If you want to store for example a UNC path to a file rather than a drive letter, you’ll need to convert the “Z” drive to the corresponding UNC root path. Here’s the C# code I wrote to do this:<\/p>\nusing <\/span>System;\nusing <\/span>System.Collections.Generic;\nusing <\/span>System.Linq;\nusing <\/span>System.Text;\nusing <\/span>System.Runtime.InteropServices;\nusing <\/span>System.IO;\n\nnamespace WiredPrairie.Samples<\/font><\/span>\n{\n public static class <\/span>Pathing\n <\/span>{\n [DllImport<\/span>(\"mpr.dll\"<\/span>, CharSet = CharSet<\/span>.Unicode, SetLastError = true<\/span>)]\n public static extern int <\/span>WNetGetConnection(\n [MarshalAs<\/span>(UnmanagedType<\/span>.LPTStr)] string <\/span>localName, \n [MarshalAs<\/span>(UnmanagedType<\/span>.LPTStr)] StringBuilder <\/span>remoteName, \n ref int <\/span>length);\n \/\/\/ <summary>\n \/\/\/ <\/span>Given a path, returns the UNC path or the original. (No exceptions\n <\/span>\/\/\/ <\/span>are raised by this function directly). For example, \"P:\\2008-02-29\"\n <\/span>\/\/\/ <\/span>might return: \"\\\\networkserver\\Shares\\Photos\\2008-02-09\"\n <\/span>\/\/\/ <\/summary>\n \/\/\/ <param name=\"originalPath\"><\/span>The path to convert to a UNC Path<\/span><\/param>\n \/\/\/ <returns><\/span>A UNC path. If a network drive letter is specified, the\n <\/span>\/\/\/ <\/span>drive letter is converted to a UNC or network path. If the \n <\/span>\/\/\/ <\/span>originalPath cannot be converted, it is returned unchanged.<\/span><\/returns>\n <\/span>public static string <\/span>GetUNCPath(string <\/span>originalPath)\n {\n StringBuilder <\/span>sb = new <\/span>StringBuilder<\/span>(512);\n int <\/span>size = sb.Capacity;\n\n \/\/ look for the {LETTER}: combination ...\n <\/span>if <\/span>(originalPath.Length > 2 && originalPath[1] == ':'<\/span>)\n {\n \/\/ don't use char.IsLetter here - as that can be misleading\n \/\/ the only valid drive letters are a-z && A-Z.\n <\/span>char <\/span>c = originalPath[0];\n if <\/span>((c >= 'a' <\/span>&& c <= 'z'<\/span>) || (c >= 'A' <\/span>&& c <= 'Z'<\/span>))\n {\n int <\/span>error = WNetGetConnection(originalPath.Substring(0, 2), \n sb, ref <\/span>size);\n if <\/span>(error == 0)\n { \n DirectoryInfo <\/span>dir = new <\/span>DirectoryInfo<\/span>(originalPath);\n\n string <\/span>path = Path<\/span>.GetFullPath(originalPath)\n .Substring(Path<\/span>.GetPathRoot(originalPath).Length);\n return <\/span>Path<\/span>.Combine(sb.ToString().TrimEnd(), path);\n }\n }\n }\n \n return <\/span>originalPath;\n }\n }\n}\n<\/pre>\n