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)
{
try
{
// 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 "[UNRESOLVED UNC PATH: " + uncPath + "]";
// Get a connection to the server as found in the UNC path
ManagementScope scope = new ManagementScope(@"\\" + uncParts[0] + @"\root\cimv2");
// Query the server for the share name
SelectQuery query = new SelectQuery("Select * From Win32_Share Where Name = '" + uncParts[1] + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
// Get the path
string path = string.Empty;
foreach (ManagementObject obj in searcher.Get())
{
path = obj["path"].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[i] : path + @"\" + uncParts[i];
}
return path;
}
catch (Exception ex)
{
return "[ERROR RESOLVING UNC PATH: " + uncPath + ": "+ex.Message+"]";
}
}
The function uses the ManagementObjectSearcher 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 ManagementScope 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.
March 10, 2009 at 5:05 am
[...] Get local path from UNC path [...]
April 3, 2009 at 4:24 pm
brian,
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.
April 14, 2009 at 7:46 am
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.