By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

c#.net detect media card insertion

P: n/a
hi
can anyone help me with this
i want to write a code in c#.net which detects usb storage and media
card insertion
can anyone help me with this??
thanx

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sanjana,

Your problem will be solved by the computer, so why try bother with
programming, unless making your own computer?

C# isn't meant to solve your kind of problem, its for software
devolopment!

Visually Seen #

Nov 17 '05 #2

P: n/a
me
/*
* A simple app to demonstrates how to monitor the UsbControllerDevice for
* the arrival of creation/operation events
* Downloaded from www.publicjoe.co.uk
*
* This software is provided 'as-is', without any express or implied
warranty.
* In no event will the author(s) be held liable for any damages arising
from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely.
*/

using System;
using System.Management;

class WMIEvent
{
public static void Main()
{
WMIEvent we = new WMIEvent();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new
ManagementOperationObserver();

// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //set required privilege

try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_DiskDrive' ";

w = new ManagementEventWatcher(scope, q);
w.EventArrived += new EventArrivedEventHandler(we.DiskEventArrived);
w.Start();

Console.ReadLine(); // block main thread for test purposes
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
w.Stop();
}
}

public void DiskEventArrived(object sender, EventArrivedEventArgs e)
{
//Get the Event object and display its properties (all)
foreach(PropertyData pd in e.NewEvent.Properties)
{
ManagementBaseObject mbo = null;

if(( mbo = pd.Value as ManagementBaseObject) != null)
{
Console.WriteLine("--------------Properties------------------");

foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
}
}"sanjana" <su****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
hi
can anyone help me with this
i want to write a code in c#.net which detects usb storage and media
card insertion
can anyone help me with this??
thanx

Nov 17 '05 #3

P: n/a
There are many reasons why you would want to detect this in a program, maybe
you want to wait until a USB is inserted into the computer and then retrieve
all the images of the USB, there are a thousand and one reasons why.

C# can definitely be used to solve this kind of problem.

"Visually Seen #" wrote:
sanjana,

Your problem will be solved by the computer, so why try bother with
programming, unless making your own computer?

C# isn't meant to solve your kind of problem, its for software
devolopment!

Visually Seen #

Nov 17 '05 #4

P: n/a
hi i tried th following to detect if anything is connected at the usb
port..dint work

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.QueryStrin*g);
w = new ManagementEventWatcher(scope,
q);
w.EventArrived += new
EventArrivedEventHandler(we.Us*bEventArrived);
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);
}
}
}
}
but its not working.......? wats the error..it does not detect if
a device is inserted at the usb port..





me wrote:
/*
* A simple app to demonstrates how to monitor the UsbControllerDevice for
* the arrival of creation/operation events
* Downloaded from www.publicjoe.co.uk
*
* This software is provided 'as-is', without any express or implied
warranty.
* In no event will the author(s) be held liable for any damages arising
from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely.
*/

using System;
using System.Management;

class WMIEvent
{
public static void Main()
{
WMIEvent we = new WMIEvent();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new
ManagementOperationObserver();

// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //set required privilege

try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_DiskDrive' ";

w = new ManagementEventWatcher(scope, q);
w.EventArrived += new EventArrivedEventHandler(we.DiskEventArrived);
w.Start();

Console.ReadLine(); // block main thread for test purposes
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
w.Stop();
}
}

public void DiskEventArrived(object sender, EventArrivedEventArgs e)
{
//Get the Event object and display its properties (all)
foreach(PropertyData pd in e.NewEvent.Properties)
{
ManagementBaseObject mbo = null;

if(( mbo = pd.Value as ManagementBaseObject) != null)
{
Console.WriteLine("--------------Properties------------------");

foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
}
}"sanjana" <su****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
hi
can anyone help me with this
i want to write a code in c#.net which detects usb storage and media
card insertion
can anyone help me with this??
thanx


Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.