c#.net detect media card insertion

  • Thread starter Thread starter sanjana
  • Start date Start date
S

sanjana

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
 
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 #
 
/*
* 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);
}
}
}
 
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.
 
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..
 
Back
Top