Thursday, January 24, 2008

Get UNC from mapped drives

Here is modified method of this original one, that returns UNC path.
Example:
Input: "R:\Programs";
Output: "\\CoolServer\SomeDir\Programs";

 public static string GetUniversalPath(string folderPath)
{
if (String.IsNullOrEmpty(folderPath) folderPath.IndexOf(":") > 1)
return folderPath;
if (folderPath.StartsWith(
"\\"))
{
return folderPath;
}
ManagementObjectSearcher searcher = new ManagementObjectSearcher("
SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + folderPath.Substring(0, 2) + "'
");
foreach (ManagementObject managementObject in searcher.Get())
{
string remoteName = managementObject["
RemoteName"] as String;
if (!String.IsNullOrEmpty(remoteName))
{
remoteName += folderPath.Substring(2);
return remoteName;
}
}
return folderPath;
}

No comments: