{"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

\"image\"<\/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>\n

using <\/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

<\/a><\/p>\n

All of the magic takes place in a Windows API function call, WNetGetConnection.<\/p>\n

I’m using this function so that I can verify that given two file names, I can be assured that they are pointing to the same physical location. (Is “Z:\\Backups\\Backup1.zip” the same as “\\\\home\\Personal\\Backups\\Backup1.zip”). <\/p>\n","protected":false},"excerpt":{"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: In this example, the “Z” drive is mapped to a “Personal” folder on a server named “Home”. If you want to store […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-m","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1592,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1592","url_meta":{"origin":22,"position":0},"title":"Get path or location of currently executing batch\/command file in Windows","date":"April 1, 2012","format":false,"excerpt":"I didn\u2019t know it was this simple, and am posting this information on my blog so I find it in the future, but hopefully this will help someone else! I\u2019ve created a number of batch files over the years which routinely copy files from one location to another, usually as\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2165,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/2165","url_meta":{"origin":22,"position":1},"title":"Automatic Reconnection of iSCSI Targets in Windows 10 using PowerShell","date":"April 4, 2017","format":false,"excerpt":"When my highly recommended Synology Disk Station reboots for a required update (I've got it set to automatically reboot), a shared Windows 10 PC in our house cannot always successfully reconnect to the iSCSI targets without manual intervention. Unfortunately, I haven't always noticed which has led to several features of\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2017\/04\/2017-04-04_19-07-23.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1084,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1084","url_meta":{"origin":22,"position":2},"title":"Silverlight Analog\/Retro Flip Numbers","date":"June 9, 2010","format":false,"excerpt":"I\u2019m not sure what to call these exactly. I\u2019ve created a relatively simple Silverlight 4 control that emulates the old mechanical flip style numbers (or letters) displays found at airports and in old clock-radios. I call it the RetroFlipper. It\u2019s easy to use (at least for my needs). Declare an\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2010\/06\/image.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1524,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1524","url_meta":{"origin":22,"position":3},"title":"Alternative to ApplicationSettings in .NET","date":"February 1, 2012","format":false,"excerpt":"After dealing with lost settings, an unclear upgrade path, and my own confusion surrounding the magic of Settings in a .NET client application, I decided to build my own. You\u2019re probably familiar with this UI in Visual Studio. It hasn\u2019t changed much since it was first created: A list of\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/02\/image.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1820,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1820","url_meta":{"origin":22,"position":4},"title":"Knockout binding for JavaScript route fixup","date":"January 25, 2013","format":false,"excerpt":"Part one. After the first round, I felt compelled to KnockOut the code a bit more. I\u2019d mentioned I wasn\u2019t pleased with the code exactly. It needed some refactoring. So, I\u2019ve created a new Knockout binding handler. This binding handler replaces\u00a0 named parameters with a model\u2019s properties in a path.\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1442,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1442","url_meta":{"origin":22,"position":5},"title":"Nest Thermostat API\/Protocol","date":"January 8, 2012","format":false,"excerpt":"While Nest Labs hasn\u2019t released a formal (documented & supported) API, I thought I\u2019d do a bit of digging to see how they\u2019re using the network and what might be achievable. A few things are going on, the majority as you\u2019d probably expect. The web interface is using a long\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/01\/image_thumb7.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/22"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=22"}],"version-history":[{"count":1,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":1678,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/22\/revisions\/1678"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

<\/a><\/p>\n