473,769 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to watch for USB events via WMI but can't seem to display output in winforms

I am very new to C# and have been trying to get to grips with it by
writing a small tool to detect a USB key being inserted and then backup
user specified files to the key.

I have written some code and been able to construct a WMI query and can
even get the output of this query to display to the console but not to
a TextBox on my form. I fear it is embarassingly obvious but having
looked at a whole heap of books (not read them in their entirety which
would be wiser) I am still no wiser as to why I see nothing. I mean the
'Start capture' message is displayed.

Anyway here is my code (I tired to strip out as much as possible to
isolate the problem). I would be greatfully for any advice.

using System;
using System.Drawing;
using System.Manageme nt;
using System.Windows. Forms;

public class USBWatch : Form
{
public static TextBox txtbox;
static ManagementEvent Watcher watcher = new
ManagementEvent Watcher();

public static void Main()
{
Application.Run (new USBWatch());
}

public USBWatch()
{
//Setup Main form
Text = "USB Watch ";
Size = new Size(500, 600);

//Setup main output textbox
txtbox = new TextBox();
txtbox.Parent = this;
txtbox.BorderSt yle=BorderStyle .None;
txtbox.Multilin e=true;
txtbox.Location = new Point(5, 30);
txtbox.Size = new Size(ClientSize .Width-10, ClientSize.Heig ht -
50);
txtbox.Anchor =
((System.Window s.Forms.AnchorS tyles)((((Syste m.Windows.Forms .AnchorStyles.T op
| System.Windows. Forms.AnchorSty les.Bottom) |
System.Windows. Forms.AnchorSty les.Left) |
System.Windows. Forms.AnchorSty les.Right)));
txtbox.ScrollBa rs = ScrollBars.Vert ical;
txtbox.ReadOnly = true;
txtbox.BorderSt yle = BorderStyle.Fix ed3D;

//Setup START Button
Button startbtn = new Button();
startbtn.Parent = this;
startbtn.Text= "Capture";
startbtn.Locati on = new Point(300, 5);
startbtn.Click += new EventHandler(St artCapture);

//Setup STOP Button
Button stopbtn = new Button();
stopbtn.Parent = this;
stopbtn.Text= "Stop";
stopbtn.Locatio n = new Point(400, 5);
stopbtn.Click += new EventHandler(St opCapture);
}

void StartCapture(ob ject obj, EventArgs ea)
{
txtbox.AppendTe xt("Capture Started \r\n");

//Set watcher scope
watcher.Scope = new ManagementScope ("root\\CIMV2") ;

//Bind to local machine
ConnectionOptio ns opt = new ConnectionOptio ns();
opt.EnablePrivi leges = true; //sets required privilege

//Create event query to be notified on creation of USB device
within 1 second
WqlEventQuery query = new WqlEventQuery() ;
query.EventClas sName = "__InstanceCrea tionEvent";
query.WithinInt erval = new TimeSpan(0, 0, 1); //Notifcation
within 1 second
query.Condition = @"TargetInstanc e ISA
'Win32_USBContr ollerDevice' ";
watcher.Query = query;

//Attach event hander and start watcher
watcher.EventAr rived += new
EventArrivedEve ntHandler(Displ ayEvent);
watcher.Start() ;
}

void DisplayEvent(ob ject obj, EventArrivedEve ntArgs eaa)
{
MessageBox.Show ("USB Event!");
txtbox.AppendTe xt("USB Event!");
}

void StopCapture(obj ect obj, EventArgs ea)
{
watcher.Stop();
watcher.Dispose ();
txtbox.AppendTe xt("Capture Stopped \r\n");
}
}

Jun 17 '06 #1
0 4091

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2463
by: Mark J. McGinty | last post by:
Greets, Part of the content of one of our web pages uses wingdings and Chr(239) through Chr(242) (which are little arrow outlines, though that's not really important.) It worked just fine in Windows 2000 Server, but now under Server 2003 it seems that characters above 127 get converted somehow, and our code no longer produces the desired effect.
8
7399
by: STom | last post by:
I have a C# Winforms app that has 5 Winforms, lets say A through E. A: Data entry. When data is entered here in any field, values are updated on forms C, D, E.(Not B) B: Data entry form. When data is entered here in any field, values are updated on forms C, D, E (not A). I am considering using delegates to fire events from forms A & B. In forms C, D, E I will have functions with the same signature and even the same name that just...
4
22886
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
9
2265
by: Greg Muncien | last post by:
In VB.Net, when you double click a control you see all available events of that control in a combo box. Click on one, and the interface code is right there waiting for you to type implementation. Why does this not occur in C#? I have to write the function header myself? Why does it seem C# goes out of its way to be less productive than VB.Net? Has this been fixed in 2005?
4
2343
by: Robert | last post by:
Hello. I have tried to remove the char "\" from a string that I am building in codebehind. to be used in a script tag. I have tried adding (char)34 to the string instead of the escape character, as well as @then double quotes. when i watch the string in the command window or the watch window it has the escape character in the string allways \"
4
4356
by: Colin McGuire | last post by:
Hi again, thanks everyone for your previous help. But having resolved past problems, I'm moving on to new problems :( This one is a simple winforms application with two buttons, named Button1 and Button2, on it (code below). When I press the mouse button over Button2, and don't release it, in the IDE output window it prints "Button2_MouseDown" only. If I release the mouse button over the button, over the form, or even over ANY other...
4
10451
by: dudzcom | last post by:
hi, this is the first time i've posted to this board, though i have been a devoted lurker for some time i have been working in vb.net and run into a couple problems that seem to require solutions involving what was in previous versions of vb, refered to as gotfocus/lostfocus events. however, it seems that though people online refer to them, my vb.net does not have them just to show i've not lost my mind...
11
1677
by: giddy | last post by:
hi , ok , i have a programming background but i'm new to C# . i'm also self taught so : i have a datagridview that should act differently depending on which user has signed in now is it more effecient/correct if i do this:
7
2873
by: Christian Cambier | last post by:
Hi, I have a textbox in a web user control. I then add the usercontrol to a web form. I also add a label in the web form. Now, when I press a key in the textbox, i want to display some text in the label. I tried by raising events as used in Winforms but it is not as easy as
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.