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+"]"; } }[/sourcecode] The function uses the <a href="http://msdn.microsoft.com/en-us/library/system.management.managementobjectsearcher.aspx" 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.
Pingback: Veckans länktips - 2009-03-10 | På spaning med Crusell
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.
LikeLike
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.
LikeLike
Great article!
Is it possible to see what version of Windows is running on the other end? Whether it’s SMB1 or SMB2 etc?
Cheers
Julian
LikeLike
Thanks – exactly what I was after.
LikeLike
That solution is not working for me because “path” property always is null. Any ideas?
LikeLike
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.
LikeLike
I use $local_path = gwmi win32_share | ? { $_.Name -eq $share } | select -expand path
LikeLike
this is not working when the website is hosted in iis
LikeLike
Thank you. This is also exactly what I was looking for as well.
LikeLike
Pingback: .net - Verifica del percorso di uguaglianza con .Net
Pingback: Verifying path equality with .Net
Pingback: Convert UNC path to local path in C#