添加 WHERE Name = 时出现无效查询错误

发布于 2024-09-17 19:13:39 字数 1966 浏览 8 评论 0原文

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 Name = X”,我就会收到“无效查询”错误。

我不知道出了什么问题。 (在有人问之前,是的 c:\0stuff 存在)。

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;
                }
            }
        }

If I use only "ObjQuery = new ObjectQuery("SELECT * FROM Win32_Directory");", I get no problem at all.

But as soon as I try to use "WHERE Name = X", I get an "invalid query" error.

I don't know what is wrong. (and before someone ask, yes c:\0stuff exist).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘香 2024-09-24 19:13:39

您需要使用逐字字符串 @"..." 来防止反斜杠在 C# 中被视为转义序列:

@"SELECT * FROM Win32_Directory WHERE Name = 'c:\\0stuff'"

如果没有 @ ,则实际发送的查询看起来像这样:

SELECT * FROM Win32_Directory WHERE Name = 'c:\0stuff'

请注意,反斜杠不再被正确转义。

You need to use a verbatim string literal @"..." to prevent the backslash being treated as an escape sequence in C#:

@"SELECT * FROM Win32_Directory WHERE Name = 'c:\\0stuff'"

Without the @ the query that is actually sent will look like this:

SELECT * FROM Win32_Directory WHERE Name = 'c:\0stuff'

Notice that the backslash is no longer properly escaped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文