使用 wmi c# 共享的本地路径
local path of a share using wmi c#
我正在尝试制作一个 .exe,它可以根据共享名称列出共享文件夹的物理路径(使用 WMI 查询)。
我正在尝试将共享名称作为 'Filter' (其中)添加到查询中
我遇到查询问题并得到异常:
System.Management.ManagementException: 'Invalid query '
寻找可以帮助我并解释的人
Static void Main(string[] args)
{
Console.WriteLine("ENTER SERVER NAME ");
string serverName = Console.ReadLine();
Console.WriteLine("ENTER SHARE FOLDER NAME");
string shareName = Console.ReadLine();
Dictionary<string, string> shares = GetNetworkShareDetailUsingWMI(serverName , shareName );
foreach (KeyValuePair<string, string> share in shares)
{
Console.WriteLine(share.Key + ": " + share.Value);
Console.WriteLine("-------------");
}
Console.ReadLine();
;
}
public static Dictionary<string, string > GetNetworkShareDetailUsingWMI(string serverName ,string shareName )
{
Dictionary<string, string> shares = new Dictionary<string, string>();
var path = string.Format(@"\{0}\root\cimv2", serverName);
var query = string.Format("Select * from Win32_Share where Path likeSelect * from Win32_Share where Name like '%" shareName "%'" );
ManagementScope scope = new ManagementScope(path);
scope.Connect();
ManagementObjectSearcher worker = new ManagementObjectSearcher(scope, new ObjectQuery(query));
foreach (ManagementObject share in worker.Get())
{
shares.Add(share["Name"].ToString(), share["Path"].ToString());
Console.WriteLine(shares);
}
Console.ReadLine();
return shares;
}
}
}
解决方法是这样的:
我执行查询错误
class Program
{
static void Main(string[] args)
{
Console.WriteLine("ENTER SERVER NAME ");
string serverName = Console.ReadLine();
Console.WriteLine("ENTER SHARE FOLDER NAME");
string shareName = Console.ReadLine();
Dictionary<string, string> shares = GetNetworkShareDetailUsingWMI(serverName , shareName );
foreach (KeyValuePair<string, string> share in shares)
{
Console.WriteLine(share.Key + ": " + share.Value);
Console.WriteLine("-------------");
}
Console.ReadLine();
;
}
public static Dictionary<string, string > GetNetworkShareDetailUsingWMI(string serverName ,string shareName )
{
Dictionary<string, string> shares = new Dictionary<string, string>();
var path = string.Format(@"\{0}\root\cimv2", serverName);
var query = string.Format("Select * from Win32_Share where Name like '%{0}%'", shareName);
ManagementScope scope = new ManagementScope(path);
scope.Connect();
ManagementObjectSearcher worker = new ManagementObjectSearcher(scope, new ObjectQuery(query));
foreach (ManagementObject share in worker.Get())
{
shares.Add(share["Name"].ToString(), share["Path"].ToString());
Console.WriteLine(shares);
}
Console.ReadLine();
return shares;
}
}
}
我正在尝试制作一个 .exe,它可以根据共享名称列出共享文件夹的物理路径(使用 WMI 查询)。 我正在尝试将共享名称作为 'Filter' (其中)添加到查询中 我遇到查询问题并得到异常:
System.Management.ManagementException: 'Invalid query '
寻找可以帮助我并解释的人
Static void Main(string[] args)
{
Console.WriteLine("ENTER SERVER NAME ");
string serverName = Console.ReadLine();
Console.WriteLine("ENTER SHARE FOLDER NAME");
string shareName = Console.ReadLine();
Dictionary<string, string> shares = GetNetworkShareDetailUsingWMI(serverName , shareName );
foreach (KeyValuePair<string, string> share in shares)
{
Console.WriteLine(share.Key + ": " + share.Value);
Console.WriteLine("-------------");
}
Console.ReadLine();
;
}
public static Dictionary<string, string > GetNetworkShareDetailUsingWMI(string serverName ,string shareName )
{
Dictionary<string, string> shares = new Dictionary<string, string>();
var path = string.Format(@"\{0}\root\cimv2", serverName);
var query = string.Format("Select * from Win32_Share where Path likeSelect * from Win32_Share where Name like '%" shareName "%'" );
ManagementScope scope = new ManagementScope(path);
scope.Connect();
ManagementObjectSearcher worker = new ManagementObjectSearcher(scope, new ObjectQuery(query));
foreach (ManagementObject share in worker.Get())
{
shares.Add(share["Name"].ToString(), share["Path"].ToString());
Console.WriteLine(shares);
}
Console.ReadLine();
return shares;
}
}
}
解决方法是这样的: 我执行查询错误
class Program
{
static void Main(string[] args)
{
Console.WriteLine("ENTER SERVER NAME ");
string serverName = Console.ReadLine();
Console.WriteLine("ENTER SHARE FOLDER NAME");
string shareName = Console.ReadLine();
Dictionary<string, string> shares = GetNetworkShareDetailUsingWMI(serverName , shareName );
foreach (KeyValuePair<string, string> share in shares)
{
Console.WriteLine(share.Key + ": " + share.Value);
Console.WriteLine("-------------");
}
Console.ReadLine();
;
}
public static Dictionary<string, string > GetNetworkShareDetailUsingWMI(string serverName ,string shareName )
{
Dictionary<string, string> shares = new Dictionary<string, string>();
var path = string.Format(@"\{0}\root\cimv2", serverName);
var query = string.Format("Select * from Win32_Share where Name like '%{0}%'", shareName);
ManagementScope scope = new ManagementScope(path);
scope.Connect();
ManagementObjectSearcher worker = new ManagementObjectSearcher(scope, new ObjectQuery(query));
foreach (ManagementObject share in worker.Get())
{
shares.Add(share["Name"].ToString(), share["Path"].ToString());
Console.WriteLine(shares);
}
Console.ReadLine();
return shares;
}
}
}