WMI 查询中的转义字符问题
Trouble with escape character in WMI query
processName.Name = @"\dfs\ns1\Application_Data\tiny\Development\tinyLOS\tinyLOS.exe";
string wmiQuery = string.Format("select CommandLine from Win32_Process where PathName='{0}'", processName.Name);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();
我正在尝试 运行 使用 WMI 在 C# 中编写上述代码,但在执行时我一直收到无效查询错误。我怀疑转义反斜杠是个问题,但我想我已经用 @ 解决了这个问题。我是否遗漏了一些简单的东西,因为这看起来应该可以正常执行?
(注:反斜杠去掉后执行查询)
您需要传递转义斜杠,它是 ExecutablePath
而不是 PathName
wmiQuery = @"SELECT CommandLine FROM Win32_Process WHERE ExecutablePath =
'C:\Program Files\Microsoft Security Client\msseces.exe'";
var searcher = new ManagementObjectSearcher("root\CIMV2", wmiQuery);
foreach (ManagementObject queryObj in searcher.Get())
Console.WriteLine("CommandLine: {0}", queryObj["CommandLine"]);
对于
CommandLine: "C:\Program Files\Microsoft Security Client\msseces.exe" -hide -runkey
processName.Name = @"\dfs\ns1\Application_Data\tiny\Development\tinyLOS\tinyLOS.exe";
string wmiQuery = string.Format("select CommandLine from Win32_Process where PathName='{0}'", processName.Name);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();
我正在尝试 运行 使用 WMI 在 C# 中编写上述代码,但在执行时我一直收到无效查询错误。我怀疑转义反斜杠是个问题,但我想我已经用 @ 解决了这个问题。我是否遗漏了一些简单的东西,因为这看起来应该可以正常执行?
(注:反斜杠去掉后执行查询)
您需要传递转义斜杠,它是 ExecutablePath
而不是 PathName
wmiQuery = @"SELECT CommandLine FROM Win32_Process WHERE ExecutablePath =
'C:\Program Files\Microsoft Security Client\msseces.exe'";
var searcher = new ManagementObjectSearcher("root\CIMV2", wmiQuery);
foreach (ManagementObject queryObj in searcher.Get())
Console.WriteLine("CommandLine: {0}", queryObj["CommandLine"]);
对于
CommandLine: "C:\Program Files\Microsoft Security Client\msseces.exe" -hide -runkey