Get local path from UNC path

This is not a Sitecore issue. It’s not even web related, but it was a problem I stumbled upon when working on the Continuous Integration project that we in Pentia use. In order to simplify some of the scripts I would like to resolve the local path from a UNC path. For example if I have a UNC path to a network drive called \\INTEGRATIONSERVER\projects I would like to know what the local URL for that server is, for example d:\projects\.

I looked at the internet without finding the exact solution. I found some VB sripting examples, and I decided to rewrite these scripts using C#.

This function will take a UNC path (for example \\server\share or \\server\c$\folder and return the local path (for example c:\share or c:\folder).

using System.Management;

public static string GetPath(string uncPath)
    // remove the "\\" from the UNC path and split the path
    uncPath = uncPath.Replace(@"\\", "");
    string[] uncParts = uncPath.Split(new char[] {'\\'}, StringSplitOptions.RemoveEmptyEntries);
    if (uncParts.Length < 2)
      return "&#91;UNRESOLVED UNC PATH: " + uncPath + "&#93;";
    // Get a connection to the server as found in the UNC path
    ManagementScope scope = new ManagementScope(@"\\" + uncParts&#91;0&#93; + @"\root\cimv2");
    // Query the server for the share name
    SelectQuery query = new SelectQuery("Select * From Win32_Share Where Name = '" + uncParts&#91;1&#93; + "'");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    // Get the path
    string path = string.Empty;
    foreach (ManagementObject obj in searcher.Get())
      path = obj&#91;"path"&#93;.ToString();

    // Append any additional folders to the local path name
    if (uncParts.Length > 2)
      for (int i = 2; i < uncParts.Length; i++)
        path = path.EndsWith(@"\") ? path + uncParts&#91;i&#93; : path + @"\" + uncParts&#91;i&#93;;

    return path;
  catch (Exception ex)
    return "&#91;ERROR RESOLVING UNC PATH: " + uncPath + ": "+ex.Message+"&#93;";

The function uses the <a href="" target="_blank">ManagementObjectSearcher</a> to search for shares on the network server. If you do not have read access to this server, you will need to log in using different credentials. Replace the line with the <strong>ManagementScope</strong> with the following lines:

ConnectionOptions options = new ConnectionOptions();
options.Username = "username";
options.Password = "password";
ManagementScope scope = new ManagementScope(@"\\" + uncParts[0] + @"\root\cimv2", options);

The function is not perfect as it searches for shares only. If you give an UNC path to a folder that does not exist, the function will return the local path to that folder if the server and share exists. For example, let’s say that the folder \\SERVER\c$\myfolder does not exist, the fucntion still returns c:\myfolder.


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in General .NET and tagged , . Bookmark the permalink.

10 Responses to Get local path from UNC path

  1. Pingback: Veckans länktips - 2009-03-10 | På spaning med Crusell

  2. mona says:

    this code is not working for me. maybe im missing something, your help would be appreciated. it runs fine with no errors. however, it is not returning the correct value of the mapped drive. foe example i have a server called serverOne on my network and i’ve got its c crive mapped on my machine and ive given it a drive letter of X. now in my code i am given the UNC path for the server- \\serverOne\folder1\subfolder1 and i would like to convert that value to x:\serverOne\folder1\subfolder1 for use in my app. you program is returning c:\serverOne\folder1\subfolder1. i’m not sure why this discrepancy is occuring.
    Again, any help is appreciated.

  3. briancaos says:

    You have misunderstood the purpose of the code. It will give you the adress as seen from the server side, not the client side.
    If you look for the adress on \\serverone, if will give you the drive on the \\serverone, not any drives mapped in the client.

  4. Julian Ross says:

    Great article!

    Is it possible to see what version of Windows is running on the other end? Whether it’s SMB1 or SMB2 etc?


  5. Ben says:

    Thanks – exactly what I was after.

  6. sun says:

    That solution is not working for me because “path” property always is null. Any ideas?

  7. casiii says:

    Isn’t there any “security” problems by knowing the physical path of a UNC path ?
    I mean shared folders are used in order to not knowing the physical path.

  8. anon says:

    I use $local_path = gwmi win32_share | ? { $_.Name -eq $share } | select -expand path

  9. Nagendra says:

    this is not working when the website is hosted in iis

  10. Thank you. This is also exactly what I was looking for as well.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.