

Result = Path.Combine(root, path.Replace(Path.GetPathRoot(path), "")) String root = lpTargetPath.ToString().Remove(0, substPrefix.Length) If (lpTargetPath.ToString().StartsWith(substPrefix)) If drive is substed, the result will be in the format of "\?\C:\RealPath\". If (0 != QueryDosDevice(lpDeviceName, lpTargetPath, lpTargetPath.Capacity)) String lpDeviceName = pathRoot.Replace("\\", "") Ĭonst String substPrefix = lpTargetPath = new StringBuilder(260) String pathRoot = Path.GetPathRoot(path) Static String GetPhysicalPath(String path) Private static extern uint QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax) You can use the QueryDosDevice to get the path of a SUBSTed SUBST uses DefineDosDevice (XP and later) to create the drive/path I'm looking for a function which will return "c:\test" when I pass in
#Unc path vs mapped drive how to
This post describe how to get the absolute path of a drive which is mapped to a local folder?įor example, I have a "c:\test" folder and an "x:" drive which is New ManagementObjectSearcher(selectQuery) įoreach (ManagementObject disk in searcher.Get()) MSDN : WMI Queries example: using System I did this using the stuff given from MSDN and bits from ibram's or Vermis' answers, though it was a bit difficult to find specific examples on the MSDN. It should be usable in case you're getting that exception that was mentioned above. Were your network drives mapped correctly?") Throw new DirectoryNotFoundException("The drive " + mappedDrive + " was not found. Throw new DirectoryNotFoundException("The drive " + mappedDrive + " was found, but is not a network drive. ServerName = Convert.ToString(networkDrive) NetworkDrive = new ManagementObject(path) If (path.ToString().Contains(mappedDrive)) Check each disk, determine if it is a network drive, and then return the real server path.įoreach (ManagementObject disk in driveSearcher.Get()) Soem variables to be used inside and out of the foreach. ManagementObjectSearcher driveSearcher = new ManagementObjectSearcher(selectWMIQuery) SelectQuery selectWMIQuery = new SelectQuery("Win32_LogicalDisk") Query to return all the local computer's drives. Private string CheckUNCPath(string mappedDrive)

/ The server path that the drive maps to ~ "////XXXXXX//ZZZZ" / Given a local mapped drive letter, determine if it is a network drive. Instead, I discovered I could query for all the drives currently on the computer and then loop through them, like so: using System.IO //For DirectoryNotFound exception. I could not replicate ibram's or Vermis' answer due to the problem I mentioned in a comment under Vermis' answer, about a type initializer exception. (in short: administrator has a different user context, so no access to mapped drives of normal user) Here is an explanation of this behaviour: Return Convert.ToString(mo) Įxplicitly running as administrator will not show mapped drives. / Console.WriteLine(string.Format("'", path ) ) / // Mapped drive pointing to \\workstation\Temp / // Example/Test paths, these will need to be adjusted to match your environment. / using System.Management // Reference / If a local drive path or a UNC network path are passed in, they will just be returned. / A static class to help with resolving a mapped drive path to a UNC network path. I've probably over documented it, but it should be self-explanatory. I expanded on ibram's answer and created this class (which has been updated per comment feedback).
