获取有关在系统中插入和移除新设备的信息 [英] Getting the information about insertion and removal of new device to the system

查看:67
本文介绍了获取有关在系统中插入和移除新设备的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


当我对我的应用程序进行竞争时,我的应用程序将基于许可证运行.许可证以USB设备的形式提供.当我的应用程序运行时,如果我删除了许可证,那么我的应用程序应该退出.
我使用以下代码来检测USB设备的插入和移除.

Hi,
As i compeleted my Application.And my application wil run based on the License.The License is Given in the form of USB device. While my application is running,inbetween if i removed my license,then my application should get exit.
I used the following code to detect the insertion and removal of Usb devices.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Management;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        const string strCardReader = "XYZ";
        public const int WM_DEVICECHANGE = 0x0219;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {            
            switch (m.Msg)
            {
                case WM_DEVICECHANGE:
                    OnDeviceChange();
                    break;
            }
            base.WndProc(ref m);
            // base.WndProc(ref m);
        }
        void OnDeviceChange()
        {
            List<string> list = new List<string>();
            ManagementObjectSearcher searcher =
            new ManagementObjectSearcher("SELECT * FROM Win32_USBControllerDevice");
            foreach (ManagementObject mo in searcher.Get())
            {
                string dependent = mo["Dependent"].ToString();
                string deviceId = dependent.Split(''='')[1];
                deviceId = deviceId.Replace(''\"'', ''\'''');
                ManagementObjectSearcher searcher2 =
                new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity Where DeviceID = " + deviceId);
                foreach (ManagementObject mo2 in searcher2.Get())
                {
                   string Description = mo2["Description"].ToString();
                   list.Add(Description);
                }
            }
            // remove duplicates, sort alphabetically and convert to array
            //string[] usbDevices = list.Distinct().OrderBy(s => s).ToArray();
            if (!list.Contains(strCardReader))
                label1.Text = "Not Available";
            else
                label1.Text = "";
        }
    }
}


但是我的问题是,一旦我从系统中删除了许可证,存储设备描述的列表就不会刷新.(仍然显示许可证可用的列表)这是由于系统刷新时间的延迟...... .它在不同的系统上有所不同.....我不知道如何解决此问题.....一旦我删除了许可证,列表就不应显示许可证名称.

[edit]插入代码块以保留格式-OriginalGriff [/edit]


But my problem is the list which stores the description of the devices is not getting refreshed once i removed the Lisence from the System.(List Still Showing the License is available)which is due to delay in system refreshing time....... It Varies on different System.....I dono how to overcome this problem........once i removed my license the list should not show the name of the license.

[edit]Code block inserted to preserve formatting - OriginalGriff[/edit]

推荐答案

您要处理InstanceCreationEventInstanceDeletionEvent事件.然后使用WMI确定更改了什么.
You want to handle the InstanceCreationEvent and InstanceDeletionEvent events. Then use WMI to determine what changed.


这篇关于获取有关在系统中插入和移除新设备的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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