469,609 Members | 1,666 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

detect USB device

Hi, i have a windows application in vb.net. I just want to detect the USB pen port whether is it connected or not, if connected then i want to copy and remove the file from the pen derive.
Mar 21 '07 #1
4 2960
sani723
117 100+
use the system.management
Mar 21 '07 #2
use the system.management
will you please give me an example.
Mar 21 '07 #3
sani723
117 100+
may be it work for you

class usbdetect
{
public static void Main()
{
usbdetect we = new usbdetect();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new
ManagementOperationObserver();
// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //sets required privilege
try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_USBController'";

Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.UsbEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
w.Stop();
}
}
public void UsbEventArrived(object sender, EventArrivedEventArgs e)
{
//Get the Event object and display it
foreach(PropertyData pd in e.NewEvent.Properties)
{
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null)
{
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
}

}
Mar 21 '07 #4
sani723
117 100+
or check this http://www.icsharpcode.net/OpenSourc...b/default.aspx


this one is for printer so modify it for usb


using System.Management;
namespace zedilabs.com
{
class PrinterOffline
{
[STAThread]
static void Main(string[] args)
{
// Set management scope
ManagementScope scope = new ManagementScope(@"\root\cimv2");
scope.Connect();

// Select Printers from WMI Object Collections
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_Printer");

string printerName = "";
foreach (ManagementObject printer in searcher.Get())
{
printerName = printer["Name"].ToString().ToLower();
if (printerName.Equals(@"hp deskjet 930c"))
{
Console.WriteLine("Printer = " + printer["Name"]);
if (printer["WorkOffline"].ToString().ToLower().Equals("true"))
{
// printer is offline by user
Console.WriteLine("Your Plug-N-Play printer is not connected.");
}
else
{
// printer is not offline
Console.WriteLine("Your Plug-N-Play printer is connected.");
}
}
}
}
}
}
Mar 21 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Stephane Belzile | last post: by
1 post views Thread by John Rauhe | last post: by
9 posts views Thread by Paul Steele | last post: by
1 post views Thread by =?Utf-8?B?WnVuZSBNb3RpZmVy?= | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.