简体   繁体   English

C#中的WMI查询在非英语机器上不起作用

[英]WMI query in C# does not work on NON-English Machine

I am creating an application that needs to track when a process starts, then raise an event when it's finished. 我正在创建一个应用程序,该程序需要跟踪进程何时开始,然后在完成时引发事件。

I have code that works perfectly, and does exactly what I need on an English machine, but when I run the same application on a French language machine it fails. 我的代码可以完美运行,并且完全可以在英语计算机上满足我的需要,但是当我在法语计算机上运行相同的应用程序时,它会失败。

here's the code that fails 这是失败的代码

qstart = new WqlEventQuery("__InstanceCreationEvent",
            new TimeSpan(0, 0, 0, 0, 5),
            "TargetInstance isa \"Win32_Process\"");

qstop = new WqlEventQuery("__InstanceDeletionEvent",
            new TimeSpan(0, 0, 0, 0, 5),
            "TargetInstance isa \"Win32_Process\"");
        try
        {
            using (wstart = new ManagementEventWatcher(qstart))
            {
                wstart.EventArrived += new EventArrivedEventHandler(ProcessStarted);
                Log.DebugEntry("BeginProcess() - Starting wstart Event");
                wstart.Start();
            }
        }
        catch (Exception ex)
        {
            Log.DebugEntry("error on wstart: " + ex.Message);
        }

        using (wstop = new ManagementEventWatcher(qstop))
        {
            wstop.EventArrived += new EventArrivedEventHandler(ProcessStopped);
            Log.DebugEntry("BeginProcess() - Starting wstop Event");
            wstop.Start();
        }

the error hits when it tries to start the query: wstart.Start(); 尝试启动查询时出现错误:wstart.Start();

and does the same for wstop.Start(); 并对wstop.Start()做同样的事情;

I can only guess it has something to do with the language and the query string, but I'm clutching at straws. 我只能猜测它与语言和查询字符串有关,但我抓紧了稻草。

The error it comes up with is: "demande non analysable" 出现的错误是: “需求不可分析”

Any help is gratefully recieved! 感谢您的任何帮助!

Martyn 马丁

Edit: Tested on 2 identical Machines, only difference being the language chosen on first startup. 编辑:在2台相同的计算机上进行了测试,唯一的不同是首次启动时选择的语言。

Apparently its because the interval you specified is too small... I just tried it on a French Windows XP SP3, and got the same error. 显然是因为您指定的间隔太小...我刚在法语Windows XP SP3上尝试过,却遇到了同样的错误。 But if I change the interval to 1 second instead, it works fine... It seems you can't specify an interval smaller than 1 second. 但是,如果我将间隔更改为1秒,则可以正常工作……似乎您不能指定小于1秒的间隔。 Not sure why this only happens on a non-English OS, though... 不确定为什么这只会在非英语操作系统上发生,但是...

EDIT: actually I just realized it's probably a bug in WqlEventQuery . 编辑:实际上我只是意识到这可能是WqlEventQuery的错误。 The qstart.QueryString looks like that with CurrentCulture = "en-US" : qstart.QueryString类似于CurrentCulture =“ en-US”:

select * from __InstanceCreationEvent within 0.005 where TargetInstance isa "Win32_Process"

But with CurrentCulture = "fr-FR" it looks like that: 但是使用CurrentCulture =“ fr-FR”时,它看起来像这样:

select * from __InstanceCreationEvent within 0,005 where TargetInstance isa "Win32_Process"

(note the difference in the number format) (请注意数字格式的差异)

So apparently the code in WqlEventQuery doesn't force the use of the invariant culture to format the number, making the query incorrect in cultures where the decimal separator is not "." 因此,显然WqlEventQuery中的代码不会强制使用不变区域性来格式化数字,从而使查询在小数点分隔符不为“”的区域性中不正确。

If you force the CurrentCulture to CultureInfo.Invariant , the query works fine, even on a French OS. 如果将CurrentCulture强制为CultureInfo.Invariant ,则即使在法语OS上,查询也可以正常工作。 You can also write the WQL query manually... 您也可以手动编写WQL查询...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM