473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

c#.net detect media card insertion

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
4 5769
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Viper99 | last post by:
Need to detect (receive notification) for the insertion/removal of PCMCIA devices on Windows 2000/XP in a C# .Net application. Was able to do this in ATL COM app using CWnd::OnDeviceChange().
10
by: Ben Xia | last post by:
Is there some way can detect MAC address with PHP? any help will be appreciate. Ben
9
by: Paul Steele | last post by:
I am writing a C# app that needs to periodically poll for cdroms and usb storage device insertions. I've looked at the WMI functions but haven't found anything all that useful. The closest is...
2
by: FE | last post by:
Hi, I need to create a program that will connect a media stream (a server on the internet - Windows Media format) and then generate an error when : * The stream is ok but there is no sound...
3
by: Mike Joyce | last post by:
I am trying to write a portable script that will find removable media, such as compact flash, sd card, usb, etc. drive and then upload files from the media. I want this to be portable so that I can...
1
by: Steve Marshall | last post by:
Hi All, Apologies if this has come up before, but how can I set up something that will notify me when a removeable drive (like a USB drive or CompactFlash card) is inserted into its slot? ...
0
by: Tim Kelley | last post by:
Is there a facility in C# to detect the insertion of an SD or CF card? Thanks
1
by: =?Utf-8?B?Sm9lIE1pbGxlcg==?= | last post by:
I am a developer and a photographer, and I am trying to write a windows service that can recieve a notification that a compact flash card, or an SD card has been insterted into a card reader slot. ...
2
by: lazzypink | last post by:
hi guys! i having a problem in my project which is create a media player and let the user choose the sound card for audio output. Let say i have 2 sound card in my computer, i want to select the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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,...
0
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...

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.