Click here to Skip to main content
15,909,827 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want a full code which is runnable at visual studio 2012 , please help me ...
Posted

1 solution

Seriously? You think CP will provide you a whole code that you haven't tried a penny?!
You need to search at least something or need some sense on how to search something. Anyway, some valuable resoruce for you:
A USB Library to Detect USB Devices[^]
C# detect usb device ClassCode[^]
 
Share this answer
 
Comments
Member 11339046 29-Dec-14 6:26am    
Hey Ridoy ,
Dude thanks for your links , actually i tried a lot , but can't find any thing and i am just a beginner in C# ... Sorry , dude but thanks for your help...
ridoy 29-Dec-14 6:40am    
Not necessary to be sorry, hope my links will help you someway. Anyway, thanks for a 1 vote!
Member 11339046 2-Jan-15 4:31am    
Hi Ridoy,
Dude i am done with the detection of the usb device ,, now i want that to classify the inserted device according to their product id , vendor id . please help me through code ..
ridoy 2-Jan-15 9:03am    
Look: http://stackoverflow.com/questions/20474324/getting-vendor-id-and-product-id-from-winusb, http://stackoverflow.com/questions/11646212/get-product-id-and-vendor-id, http://stackoverflow.com/questions/18246711/passing-product-id-and-vendor-id
Member 11339046 3-Jan-15 4:07am    
Hi Ridoy ,, Thanks for those links provided by you . With your help I made this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics.Contracts;
using System.IO.Ports;
using System.Management;
using System.Management.Instrumentation;
using System.IO;

namespace USBDetactor
{
class Program
{
static void Main(string[] args)
{
AddInsertUSBHandler();
AddRemoveUSBHandler();
while (true)
{
}

}
static ManagementEventWatcher w = null;

static void AddRemoveUSBHandler()
{

WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;

try
{

q = new WqlEventQuery();
q.EventClassName = "__InstanceDeletionEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += USBRemoved;

w.Start();
}
catch (Exception e)
{


Console.WriteLine(e.Message);
if (w != null)
{
w.Stop();

}
}

}

static void AddInsertUSBHandler()
{

WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;

try
{

q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0, 0, 3);
q.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'";
w = new ManagementEventWatcher(scope, q);
w.EventArrived += USBInserted;

w.Start();
}
catch (Exception e)
{

Console.WriteLine(e.Message);
if (w != null)
{
w.Stop();
}
}

}

static void USBInserted(object sender, EventArgs e)
{

Console.WriteLine("A USB device inserted");
Logger logevents = new Logger();
var devices=logevents.GetUSBDevices();
logevents.LogInfo("DEVICE INSERTED " + Environment.NewLine + " TIME: " + DateTime.Now, devices);
}

static void USBRemoved(object sender, EventArgs e)
{
Console.WriteLine("A USB device removed");
Logger logevents = new Logger();
var devices = logevents.GetUSBDevices();
logevents.LogInfo("DEVICE REMOVED " + Environment.NewLine + " TIME: " + DateTime.Now, devices);
}
public static void RetrieveUSBDevices(int vid, int pid)
{
var usbFinder = new UsbDeviceFinder(vid, pid); //// Error 1 : The type or namespace name 'UsbDeviceFinder' cnnot be found(are you missing a using directive or assembly reference.)

var usbDevices = new UsbRegDeviceList(); /////// Error 2 : The type or namespace name 'UsbRegDeviceList' cnnot be found(are you missing a using directive or assembly reference.)

var en = usbDevices.GetEnumerator();

while (en.MoveNext())
{
Console.WriteLine(en.ToString());
}


}

}
}
Please suggest me what is my fault and how can i handle these things ...

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