: UNC-
. UNC- (,\server\share \server\c $\ folder (, c:\share c:\folder).
using System.Management;
public static string GetPath(string uncPath)
{
try
{
uncPath = uncPath.Replace(@"\\", "");
string[] uncParts = uncPath.Split(new char[] {'\\'}, StringSplitOptions.RemoveEmptyEntries);
if (uncParts.Length < 2)
return "[UNRESOLVED UNC PATH: " + uncPath + "]";
ManagementScope scope = new ManagementScope(@"\\" + uncParts[0] + @"\root\cimv2");
SelectQuery query = new SelectQuery("Select * From Win32_Share Where Name = '" + uncParts[1] + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
string path = string.Empty;
foreach (ManagementObject obj in searcher.Get())
{
path = obj["path"].ToString();
}
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+"]";
}
}
ManagementObjectSearcher . , , . ManagementScope :
ConnectionOptions options = new ConnectionOptions();
options.Username = "username";
options.Password = "password";
ManagementScope scope = new ManagementScope(@"\\" + uncParts[0] + @"\root\cimv2", options);