Click here to Skip to main content
16,022,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 = "";
        }
    }
}

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]
Posted
Updated 20-Aug-10 23:49pm
v3

1 solution

You want to handle the InstanceCreationEvent and InstanceDeletionEvent events. Then use WMI to determine what changed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900