使用C#监视WMI文件 [英] WMI Files Montioring using C#

查看:156
本文介绍了使用C#监视WMI文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C#中的 WMI(Windows管理规范),并且停留在某个位置。

I am working on WMI(Windows Management Instrumentation) in C# and stuck at a point.

我必须使用 WMI(C#)创建类似于文件系统监视程序的应用程序。

I have to create an application using WMI (C#) similar to File System Watcher.

我想每当在特定文件夹中创建或删除新文件时,都会得到通知。

I would like to get notified every time whenever within a particular folder a new file is created or deleted.

我的WQL查询是:

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

使用 wbemtest 运行查询时,它会显示一条错误消息,提示无效的类。

While running the query using wbemtest , it displays an Error message prompting Invalid Class.

有人可以帮我吗?

推荐答案

以便检测何时创建,修改或删除文件时,必须使用 __ InstanceOperationEvent WMI类以及使用 __ Class 属性的值可以得出

In order to detect when a file is created , modified or deleted you must use the __InstanceOperationEvent WMI Class and the using the value of __Class property you can figure out out if the file was modified, deleted o created.

尝试此示例

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

这篇关于使用C#监视WMI文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆