WMI在C#中无效查询但在wbemtest实用程序中有效

时间:2017-01-11 17:56:01

标签: c# .net wmi wmi-query

我正在尝试创建一个WQLEventQuery,以便在C#applet中运行,只要在指定文件夹中创建新文件夹,就会触发该applet。我之前使用过WMI,我很熟悉它的工作原理。在将新文件添加到特定文件夹时,我成功创建了相同类型的事件查询。奇怪的是,我在调试中运行applet时遇到异常,但如果我采用相同的查询并从' wbemtest'运行它。内置于Windows中的实用程序,没有'无效查询'当我删除Within子句时抛出。但是,如果我没有在C#代码中的WQLEventQuery对象上设置WithinInterval属性,则会抛出与所需轮询间隔相关的异常异常。以下是上下文的一些代码片段:

FolderMonitor.cs

<?xdoxslt:get_variable($_XDOCTX, 'HEADER')?>

我的活动注册课程

using System;
using System.Configuration;
using System.Management;
using MyProject.core.interfaces;

namespace MyProject.monitor.WmiEventMonitors
{
    public class FolderMonitor : IWQLMonitor
    {
        private const string _eventClassName = "__InstanceCreationEvent";
        private const string _isaType = "Win32_SubDirectory";

        private readonly IEventListenerManager _eListenerManager;
        private readonly IFileProcessService _fileProcessService;

        public WqlEventQuery Query { get; }
        public string Path { get; }

        public FolderMonitor(string path, IEventListenerManager eListenerManager, IFileProcessService fileProcessService)
        {
            _eListenerManager = eListenerManager;
            _fileProcessService = fileProcessService;

            if (string.IsNullOrEmpty(path))
                path = GetConfiguredDirectory();

            Path = path;

            var queryParamPath = path.Replace(@"\", @"\\");

            //Query = new WqlEventQuery();
            //Query.QueryString = $@"Select * From {_eventClassName} Within 1 Where TargetInstance Isa '{_isaType}' And TargetInstance.GroupComponent = 'Win32_Directory.Name={queryParamPath}'";

            Query = new WqlEventQuery
            {
                EventClassName = _eventClassName,
                Condition = $"TargetInstance isa '{_isaType}' And TargetInstance.GroupComponent = 'Win32_Directory.Name={queryParamPath}'"
                WithinInterval = new TimeSpan(0,5,0)
            };
        }

        public void HandleEvent(object sender, EventArrivedEventArgs e)
        {
            // when a new subfolder is created:
            // 1) Log it somewhere?
            // 2) Create a new WMI listener for that subfolder to detect file creation
            string newDirPath = null;
            try
            {
                foreach (PropertyData pd in e.NewEvent.Properties)
                {
                    if (pd.Name != "TargetInstance") continue;

                    ManagementBaseObject mbo;
                    if ((mbo = pd.Value as ManagementBaseObject) != null)
                    {
                        using (mbo)
                        {
                            var newSubDir = mbo.Properties["PartComponent"];
                            var newDir = newSubDir.Value as ManagementBaseObject;
                            newDirPath = $"{newDir.Properties["Drive"].Value}{newDir.Properties["Path"].Value}";
                    }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                throw;
            }

            if (!string.IsNullOrEmpty(newDirPath))
            {
                var newFileMonitorEvent = new FileMonitor(newDirPath, _fileProcessService);
                _eListenerManager.Add(newFileMonitorEvent);
            }
        }


        private static string GetConfiguredDirectory()
        {
            return ConfigurationManager.AppSettings["Directory"].Trim();
        }

    }
}

我的WMI实用程序类

using System;
using System.Management;
using MyProject.monitor.WmiEventMonitors;

namespace MyProject.monitor
{
    public interface IFileMonitorEventRegistrar
    {
        ManagementEventWatcher RegisterEventListener(IWQLMonitor newMonitorCandidate);
        bool UnregisterEventListener(ManagementEventWatcher listener);
    }

    public class FileMonitorEventRegistrar : IFileMonitorEventRegistrar
    {
        public ManagementEventWatcher RegisterEventListener(IWQLMonitor newMonitorCandidate)
        {
            var scope = WmiUtility.GetConnectionScope();
            ManagementEventWatcher watcher = null;
            try
            {
                watcher = new ManagementEventWatcher(scope, newMonitorCandidate.Query);
                watcher.EventArrived += new EventArrivedEventHandler(newMonitorCandidate.HandleEvent);
                watcher.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                throw;
            }
            return watcher;
        }

        public bool UnregisterEventListener(ManagementEventWatcher listener)
        {
            listener.Stop();
            listener.Dispose();

            return true;
        }
    }
}

我的EventQuery Manager类

using System;
using System.Management;

namespace MyProject.monitor
{
    public static class WmiUtility
    {
        private static ManagementScope _connectionScope;
        private static ConnectionOptions _connectionOptions;


        public static ManagementScope GetConnectionScope()
        {
            EstablishConnection(null, null, null, Environment.MachineName);
            return _connectionScope;
        }


        private static ConnectionOptions SetConnectionOptions()
        {
            return new ConnectionOptions
            {
                Impersonation = ImpersonationLevel.Impersonate,
                Authentication = AuthenticationLevel.Default,
                EnablePrivileges = true
            };
        }


        private static ManagementScope SetConnectionScope(string machineName, ConnectionOptions options)
        {
            ManagementScope connectScope = new ManagementScope();
            connectScope.Path = new ManagementPath(@"\\" + machineName + @"\root\CIMV2");
            connectScope.Options = options;

            try
            {
                connectScope.Connect();
            }
            catch (ManagementException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                throw;
            }
            return connectScope;
        }


        private static void EstablishConnection(string userName, string password, string domain, string machineName)
        {
            _connectionOptions = SetConnectionOptions();
            if (domain != null || userName != null)
            {
                _connectionOptions.Username = domain + "\\" + userName;
                _connectionOptions.Password = password;
            }
            _connectionScope = SetConnectionScope(machineName, _connectionOptions);
        }
    }
}

orchestrator类

using System;
using System.Collections.Generic;
using System.Management;
using MyProejct.monitor.WmiEventMonitors;

namespace MyProject.monitor
{
    public interface IEventListenerManager : IDisposable
    {
        IDictionary<string, ManagementEventWatcher> RegisteredEvents { get; }
        bool Add(IWQLMonitor eListener);
        bool Remove(string monitoredPath);
    }

    public class EventListenerManager : IEventListenerManager
    {
        private bool _disposed;

        private readonly IFileMonitorEventRegistrar _eventRegistrar;

        public IDictionary<string, ManagementEventWatcher> RegisteredEvents { get; }


        public EventListenerManager(IFileMonitorEventRegistrar eventRegistrar)
        {
            _eventRegistrar = eventRegistrar;
            RegisteredEvents = new Dictionary<string, ManagementEventWatcher>();
        }


        public bool Add(IWQLMonitor eListener)
        {
            RegisteredEvents.Add(eListener.Path, _eventRegistrar.RegisterEventListener(eListener));
            return true;
        }

        public bool Remove(string monitoredPath)
        {
            if (RegisteredEvents.ContainsKey(monitoredPath))
            {
                _eventRegistrar.UnregisterEventListener(RegisteredEvents[monitoredPath]);
                return RegisteredEvents.Remove(monitoredPath);
            }
            return true;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    foreach (var item in RegisteredEvents)
                    {
                        Remove(item.Key);
                    }
                }

                _disposed = true;
            }
        }


        public void Dispose()
        {
            Dispose(true);
        }
    }
}

所以查询字符串本质上是 &#34;从1中的__InstanceCreationEvent中选择*,其中TargetInstance是&#39; Win32_SubDirectory&#39;和TargetInstance.GroupComponent =&#39; Win32_Directory.Name = C:\\ MonitoredDocs&#39;&#34;

如果获取该查询字符串并删除within子句,则wbemtest将其接受为有效的WMI查询。当within子句存在时,它表示它是无效的查询。我正在使用this article的答案。任何有关如何使这个WQL事件查询工作的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

所以我阅读了上面发布的文章,注意力更集中,并找到了一个实际可行的备用查询。查询字符串如下所示:

从__InstanceCreationEvent中选择* From TargetInstance Isa&#39; Win32_Directory&#39;而TargetInstance.Drive =&#39; C:&#39;并且TargetInstance.Path =&#39; \\ path \\到\\ monitored \\目录\\&#39;

*一个令人困惑的细节是路径值必须包含尾随&#34; \\&#34;

在代码方面,我修改过的FolderMonitor.cs类如下所示:(标有*!*!*的更改)

public class FolderMonitor : IWQLMonitor
    {
        private const string _eventClassName = "__InstanceCreationEvent";

        *!*!*private const string _isaType = "Win32_Directory";*!*!*

        private readonly IEventListenerManager _eListenerManager;
        private readonly IFileProcessService _fileProcessService;

        public WqlEventQuery Query { get; }
        public string Path { get; }

        public FolderMonitor(string path, IEventListenerManager eListenerManager, IFileProcessService fileProcessService)
        {
            _eListenerManager = eListenerManager;
            _fileProcessService = fileProcessService;

            if (string.IsNullOrEmpty(path))
                path = GetConfiguredDirectory();

            Path = path;

            *!*!*var drive = Path.Substring(0, 2);*!*!*
            *!*!*var queryPath = Path.Substring(2) + @"\";*!*!*

            var queryParamPath = queryPath.Replace(@"\", @"\\");

            Query = new WqlEventQuery
            {
                EventClassName = _eventClassName,

                *!*!*Condition = $"TargetInstance Isa '{_isaType}' And TargetInstance.Drive = '{drive}' And TargetInstance.Path = '{queryParamPath}'",*!*!*

                WithinInterval = new TimeSpan(0,0,1)
            };
        } 
    }