/// <summary>
/// 获取本地共享文件夹网络路径,逐级向上查询
/// </summary>
/// <param name="localPath">文件夹路径</param>
/// <returns>item1:共享文件夹名;item2:共享文件夹路径</returns>
private Tuple<string, string> GetShareName(string localPath)
{
Tuple<string, string> t = new Tuple<string, string>(null, null);
var escapedPath = localPath.Replace(@"", @"\");
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("\root\CIMV2", $"select Name from Win32_Share where Path="{escapedPath}""))
using (var items = searcher.Get())
{
foreach (ManagementObject item in items)
{
t = new Tuple<string, string>(item["Name"].ToString(), localPath);
item.Dispose();
}
}
if (t.Item1 == null && Path.GetDirectoryName(localPath) != null)
{
t = GetShareName(Path.GetDirectoryName(localPath));
}
return t;
}
这个是自己百度后搞出来的,在方法调用处,直接将原地址replace(t.item2,t.item1),前面拼上IP地址就可以得到文件夹的网络路径了
求一个简单的方法,感觉文件夹的网络路径这个属性应该是能直接读出来的