3
public static void Command(string vCommand, string machineName, string username, string password)
{
ManagementScope Scope = null;
ConnectionOptions ConnOptions = null;
ObjectQuery ObjQuery = null;
ManagementObjectSearcher ObjSearcher = null;
try
{
ConnOptions = new ConnectionOptions();
ConnOptions.Impersonation = ImpersonationLevel.Impersonate;
ConnOptions.EnablePrivileges = true;
//local machine
if (machineName.ToUpper() == Environment.MachineName.ToUpper())
Scope = new ManagementScope(@"\ROOT\CIMV2", ConnOptions);
else
{
//remote machine
ConnOptions.Username = username;
ConnOptions.Password = password;
Scope = new ManagementScope(@"\\" + machineName + @"\ROOT\CIMV2", ConnOptions);
}
Scope.Connect();
ObjQuery = new ObjectQuery("SELECT * FROM Win32_Directory WHERE Name = 'c:\\0stuff'");
ObjSearcher = new ManagementObjectSearcher(Scope, ObjQuery);
foreach (ManagementObject obj in ObjSearcher.Get()) //ERROR HAPPEN HERE
{
//code here
}
if (ObjSearcher != null)
{
ObjSearcher.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
如果我只使用「ObjQuery = new ObjectQuery(」SELECT * FROM Win32_Directory「);」,我完全沒有問題。當我添加WHERE時出現無效查詢錯誤名稱=
但是,當我嘗試使用「WHERE Name = X」時,出現「無效查詢」錯誤。
我不知道什麼是錯的。 (並且在有人問之前,是否存在c:\ 0stuff)。
它的工作原理!非常感謝。我該怎麼知道我必須爲C#和SQL雙反斜槓> _ < 這是我不會忘記的東西(我希望) – Wildhorn 2010-09-01 19:00:53