1.获取本地路径的网络访问地址
private IEnumerable<KeyValuePair<string, string>> GetShareFolders()
{
var shareFolders = new List<KeyValuePair<string, string>>(); // Create a ManagementClass object
var searcher = new ManagementObjectSearcher("select * from win32_share"); foreach (ManagementObject share in searcher.Get())
{
//共享目录的名字
string name = string.Empty; PropertyData propertyData = share.Properties["Name"];
if (propertyData.Value != null && !string.IsNullOrEmpty(propertyData.Value.ToString()))
{
name = propertyData.Value.ToString();
} //共享目录的本地路程
string path = string.Empty; propertyData = share.Properties["Path"];
if (propertyData.Value != null && !string.IsNullOrEmpty(propertyData.Value.ToString()))
{
path = propertyData.Value.ToString();
} if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(path))
{
var kv = new KeyValuePair<string, string>(name, path); shareFolders.Add(kv);
}
} return shareFolders;
} private string GetShareFolderPath(string path)
{
var kvList = GetShareFolders(); var kv = kvList.FirstOrDefault(item => item.Value.Equals(path)); if (!string.IsNullOrEmpty(kv.Key))
{
return @"\\" + Dns.GetHostName() + @"\" + kv.Key;
} return null;
}