Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WMI文件监视使用C#_C#_.net_C# 4.0_C# 3.0_Wmi - Fatal编程技术网

WMI文件监视使用C#

WMI文件监视使用C#,c#,.net,c#-4.0,c#-3.0,wmi,C#,.net,C# 4.0,C# 3.0,Wmi,我正在C#中使用WMI(Windows管理工具),并在某一点上卡住了 我必须使用类似于文件系统监视程序的WMI(C#)创建一个应用程序 每当在特定文件夹中创建或删除新文件时,我都希望收到通知 我的WQL查询是: SELECT * from _InstanceModificationEvent within 2 where TargetInstance ISA 'CIM_DataFile' and TargetInstance.Drive = 'C:' AND TargetInstance.Pat

我正在C#中使用WMI(Windows管理工具),并在某一点上卡住了

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

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

我的WQL查询是:

SELECT * from _InstanceModificationEvent within 2 where TargetInstance ISA 'CIM_DataFile' and TargetInstance.Drive = 'C:' AND TargetInstance.Path='\\Test'
使用wbemtest运行查询时,它会显示一条错误消息,提示类无效


有人能帮我解决这个问题吗?

为了检测文件是何时创建、修改或删除的,您必须使用WMI类和使用
\uu 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();
        }
    }
}

这是打字错误吗?前缀应该有2个下划线。是的,谢谢,查询必须在通知查询中运行,而不是作为简单查询运行。但进程仍然挂起而不是执行。您能提供一些解决方案吗?谢谢RRUZ,这就是我要搜索的:)