其他分享
首页 > 其他分享> > 监控USB网卡插拔

监控USB网卡插拔

作者:互联网

原文链接:http://www.cnblogs.com/WilliamJiang/archive/2012/04/18/2455347.html
 ManagementEventWatcher getEventWatcher = null;
            WqlEventQuery getEventQuery = null;
            ManagementOperationObserver getObserver = new ManagementOperationObserver();
            ManagementScope getScope = new ManagementScope("root\\CIMV2");
            getScope.Options.EnablePrivileges = true;

            try
            {
                getEventQuery = new WqlEventQuery();
                getEventQuery.EventClassName = "__InstanceOperationEvent";
                getEventQuery.WithinInterval = new TimeSpan(0, 0, 0, 1);//查询的频率
                getEventQuery.Condition = @"TargetInstance ISA 'Win32_NetworkAdapter'";//设置查询条件
                getEventWatcher = new ManagementEventWatcher(getEventQuery);
                getEventWatcher.EventArrived += new EventArrivedEventHandler(getEventWatcher_EventArrived);
                getEventWatcher.Start();//Start Watch Event

下面是 getEventWatcher_EventArrived

        void getEventWatcher_EventArrived(object sender, EventArrivedEventArgs e)
        {

            ManagementBaseObject getBaseObject = (ManagementBaseObject)e.NewEvent;
            if ((getBaseObject.ClassPath.ClassName == "__InstanceModificationEvent"))
            {
                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

                bool isContains = false;
                foreach (NetworkInterface adapter in adapters)
                {

                    if (adapter.Description.Contains("HUAWEI"))
                    {
                        isContains = true;
                        break;
                    }
                    else
                    {
                        isContains = false;
                    }
                }
                if (isContains)
                {
                    startDownload();
                }
                else
                {
                    pauseDownload();
                }

            }
        }

转载于:https://www.cnblogs.com/WilliamJiang/archive/2012/04/18/2455347.html

标签:插拔,isContains,USB,EventArrived,NetworkInterface,网卡,new,getEventWatcher,getEventQ
来源: https://blog.csdn.net/weixin_30409849/article/details/99616999