'WMI query escape single quote

string dirName = @"D:\Test't";
string qry = "associators of {Win32_Directory.Name='" + dirName + "'} where assocclass = Win32_SubDirectory ResultRole = PartComponent";

While executing the above WMI query, I am getting Invalid object path exception. How to escape single quote in WMI query?



Solution 1:[1]

I had the same issue but I was using VBScript, the query I was using looked like this:

qry = "Associators of {Win32_Directory.Name='" & dirName & "'} Where AssocClass = Win32_Subdirectory ResultRole = PartComponent"

I change it to the following and the issue was fixed, I simply change the single quote to a double quote but I had to double the double quotes because that is how a double quote is escaped in VBScript.

qry = "Associators of {Win32_Directory.Name=""" & dirName & """} Where AssocClass = Win32_Subdirectory ResultRole = PartComponent"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman