1

我刚开始编写一个检测 USB 记忆棒的程序,这是我当前的代码:

public enum EventType
        {
            Inserted = 2,
            Removed = 3
        }
        static void Main(string[] args){

            ManagementEventWatcher watcher = new ManagementEventWatcher();
            WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2 or EventType = 3");

            watcher.EventArrived += (s, e) =>
            {
                string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
                EventType eventType = (EventType)(Convert.ToInt16(e.NewEvent.Properties["EventType"].Value));

                string eventName = Enum.GetName(typeof(EventType), eventType);

                Console.WriteLine("{0}: {1} {2}", DateTime.Now, driveName, eventName);
            };

            watcher.Query = query;
            watcher.Start();

            Console.ReadKey();


        } 

但每次我收到此错误时:“System.TypeInitializationException:“'System.Management.ManagementPath' 的类型初始化程序引发异常。”

我找不到解决方案。是的,我导入了 System.Management;如果有人可以帮助我,我会很高兴。

4

0 回答 0