示例#1
0
            void ChangeEnable(bool enable)
            {
                ManagementObjectSearcher searcher = null;

                try
                {
                    searcher = new ManagementObjectSearcher("root\\CIMV2",
                                                            "SELECT * FROM Win32_PnPEntity WHERE DeviceID='" +
                                                            DeviceID.Replace("\\", "\\\\") + "'");

                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        try
                        {
                            DeviceHelper.SetDeviceEnabled(Guid.Parse(ClassGuid), PNPDeviceID, enable);

                            //bool reqReboot = false;
                            //object oreqReboot;
                            //if (!enable)
                            //    oreqReboot = queryObj.InvokeMethod("Disable", new object[] { reqReboot });
                            //else
                            //    oreqReboot = queryObj.InvokeMethod("Enable", new object[] { reqReboot });
                        }
                        catch { }
                        finally { queryObj.Dispose(); }
                    }
                }
                catch { }
                finally
                {
                    if (searcher != null)
                    {
                        searcher.Dispose();
                        searcher = null;
                    }
                }
            }