【问题标题】:WMI Files Montioring using C#使用 C# 进行 WMI 文件监控
【发布时间】:2014-07-14 12:16:07
【问题描述】:

我正在使用 C# 开发 WMI(Windows Management Instrumentation),但遇到了问题。

我必须使用类似于 File System Watcher 的 WMI (C#) 创建一个应用程序。

我希望每次在特定文件夹中创建或删除新文件时都能收到通知。

我的 WQL 查询是:

SELECT * from _InstanceModificationEvent within 2 where TargetInstance ISA 'CIM_DataFile' and TargetInstance.Drive = 'C:' AND TargetInstance.Path='\\Test'

在使用 wbemtest 运行查询时,它会显示一条错误消息,提示 Invalid Class。

有人可以帮我解决同样的问题吗?

【问题讨论】:

  • _InstanceModificationEvent 中的错字吗?应该有 2 个下划线作为前缀。
  • 是的,谢谢,而且查询必须在通知查询中运行,而不是作为简单查询。但是进程仍然挂起而不是执行。您能提供一些解决方案吗??

标签: c# .net c#-4.0 c#-3.0 wmi


【解决方案1】:

为了检测文件何时被创建、修改或删除,您必须使用__InstanceOperationEvent WMI 类和使用__Class 属性的值,您可以确定文件是否被修改、删除或创建。

试试这个示例

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;


namespace GetWMI_Info
{
    public class EventWatcherAsync
    {
        private void WmiEventHandler(object sender, EventArrivedEventArgs e)
        {
            // e.NewEvent
            string wclass = ((ManagementBaseObject)e.NewEvent).SystemProperties["__Class"].Value.ToString();
            string wop = string.Empty;
            switch (wclass)
            {
                case "__InstanceModificationEvent":
                    wop = "Modified";
                    break;
                case "__InstanceCreationEvent":
                    wop = "Created";
                    break;
                case "__InstanceDeletionEvent":
                    wop = "Deleted";
                    break;
            }
            string wfilename = ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["FileName"].ToString();

            if (!string.IsNullOrEmpty(((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Extension"].ToString()))
            {
                wfilename += "." + ((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)["Extension"].ToString();
            }
            Console.WriteLine(String.Format("The File {0} was {1}", wfilename, wop));


        }

        public EventWatcherAsync()
        {
            try
            {
                string ComputerName = "localhost";
                string WmiQuery;
                ManagementEventWatcher Watcher;
                ManagementScope Scope;


                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username = "";
                    Conn.Password = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);
                Scope.Connect();
                //Check for changes in the path C:\Test
                WmiQuery = @"Select * From __InstanceOperationEvent Within 1 
                Where TargetInstance ISA 'CIM_DataFile' and TargetInstance.Drive = 'C:' AND TargetInstance.Path='\\Test\\'";

                Watcher = new ManagementEventWatcher(Scope, new EventQuery(WmiQuery));
                Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler);
                Watcher.Start();
                Console.Read();
                Watcher.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception {0} Trace {1}", e.Message, e.StackTrace);
            }

        }

        public static void Main(string[] args)
        {
            Console.WriteLine("Listening {0}", "__InstanceOperationEvent");
            Console.WriteLine("Press Enter to exit");
            EventWatcherAsync eventWatcher = new EventWatcherAsync();
            Console.Read();
        }
    }
}

【讨论】:

  • 谢谢 RRUZ,这就是我要找的东西 :)
猜你喜欢
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多