C# USB Detection [modified]
-
hi, I've tried the following code at http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic6944.aspx i've made the changes they say, but it still doesn't work when i insert a usb device (flashdisk). I'm trying do detect when a device is inserted into a usb port. how can i do it? and if possible, which port it was inserted at. it's for a WPF app EDIT: ok, it's working now, but i have the same questions that the person with the code posted: 1)Check if the device is inserted or removed..How do i do that?? 2)also i want to check Total storage (capacity)of the device connected c) storage(unused)available in the device thanks donovan -- modified at 6:54 Thursday 12th July, 2007
rather have something you don't need, than need something you don't have
-
hi, I've tried the following code at http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic6944.aspx i've made the changes they say, but it still doesn't work when i insert a usb device (flashdisk). I'm trying do detect when a device is inserted into a usb port. how can i do it? and if possible, which port it was inserted at. it's for a WPF app EDIT: ok, it's working now, but i have the same questions that the person with the code posted: 1)Check if the device is inserted or removed..How do i do that?? 2)also i want to check Total storage (capacity)of the device connected c) storage(unused)available in the device thanks donovan -- modified at 6:54 Thursday 12th July, 2007
rather have something you don't need, than need something you don't have
Maybe this can help you http://www.codeproject.com/cs/system/DriveDetector.asp ? As for storage Use GetDrives() function to get all drives and identify the drive you are interested in. Then you can use DriveInfo functions to get necessary information
#region signature my articles #endregion
-
hi, I've tried the following code at http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic6944.aspx i've made the changes they say, but it still doesn't work when i insert a usb device (flashdisk). I'm trying do detect when a device is inserted into a usb port. how can i do it? and if possible, which port it was inserted at. it's for a WPF app EDIT: ok, it's working now, but i have the same questions that the person with the code posted: 1)Check if the device is inserted or removed..How do i do that?? 2)also i want to check Total storage (capacity)of the device connected c) storage(unused)available in the device thanks donovan -- modified at 6:54 Thursday 12th July, 2007
rather have something you don't need, than need something you don't have
Hello, I'm using System.Management functionality for that!
//Insert
WqlEventQuery q_creation = new WqlEventQuery();
q_creation.EventClassName = "__InstanceCreationEvent";
q_creation.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'";
mwe_creation = new ManagementEventWatcher(q_creation);
mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
mwe_creation.Start(); // Start listen for eventsinternal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
{}
//Remove
WqlEventQuery q_deletion = new WqlEventQuery();
q_deletion.EventClassName = "__InstanceDeletionEvent";
q_deletion.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
q_deletion.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition' ";
mwe_deletion = new ManagementEventWatcher(q_deletion);
mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
mwe_deletion.Start(); // Start listen for eventsinternal void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
{}
For checking the free and available space:
public void Hashtable GetFreeDriveSpace()
{
Hashtable drivefreespace = new Hashtable();
// Get the management class holding Logical Drive information
ManagementClass mcDriveClass = new ManagementClass("Win32_LogicalDisk");
// Enumerate all logical drives available
ManagementObjectCollection mocDrives = mcDriveClass.GetInstances();
foreach(ManagementObject moDrive in mocDrives)
{
/*
Other values of DriveType property:
0 Unknown
1 No Root Directory
2 Removable Disk
3 Local Disk
4 Network Drive
5 Compact Disc
6 RAM Disk
*/
try
{
//if (int.Parse(moDrive.Properties["DriveType"].Value.ToString()) == 3) //You can check the drive type here
//{
// sDeviceId will hold the drive name eg "C:"
String sDeviceId = moDrive.Properties["DeviceId"].Value.ToString();
// dSize and dFree will hold the size of the drive and free space in bytes
double dSize = double.Parse(moDrive.Properties["Size"].Value.ToString());
double dFree = double.Parse(moDrive.Properties["FreeSpace"].Value.ToString()); //In Byte
//}
}
catch
{
}
}
mocDrives.Dispose();
mcDriveClass.Dispose();
}Hope it helps!
All the best, Martin
-
Hello, I'm using System.Management functionality for that!
//Insert
WqlEventQuery q_creation = new WqlEventQuery();
q_creation.EventClassName = "__InstanceCreationEvent";
q_creation.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
q_creation.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition'";
mwe_creation = new ManagementEventWatcher(q_creation);
mwe_creation.EventArrived += new EventArrivedEventHandler(USBEventArrived_Creation);
mwe_creation.Start(); // Start listen for eventsinternal void USBEventArrived_Creation(object sender, EventArrivedEventArgs e)
{}
//Remove
WqlEventQuery q_deletion = new WqlEventQuery();
q_deletion.EventClassName = "__InstanceDeletionEvent";
q_deletion.WithinInterval = new TimeSpan(0,0,2); //How often do you want to check it? 2Sec.
q_deletion.Condition = @"TargetInstance ISA 'Win32_DiskDriveToDiskPartition' ";
mwe_deletion = new ManagementEventWatcher(q_deletion);
mwe_deletion.EventArrived += new EventArrivedEventHandler(USBEventArrived_Deletion);
mwe_deletion.Start(); // Start listen for eventsinternal void USBEventArrived_Deletion(object sender, EventArrivedEventArgs e)
{}
For checking the free and available space:
public void Hashtable GetFreeDriveSpace()
{
Hashtable drivefreespace = new Hashtable();
// Get the management class holding Logical Drive information
ManagementClass mcDriveClass = new ManagementClass("Win32_LogicalDisk");
// Enumerate all logical drives available
ManagementObjectCollection mocDrives = mcDriveClass.GetInstances();
foreach(ManagementObject moDrive in mocDrives)
{
/*
Other values of DriveType property:
0 Unknown
1 No Root Directory
2 Removable Disk
3 Local Disk
4 Network Drive
5 Compact Disc
6 RAM Disk
*/
try
{
//if (int.Parse(moDrive.Properties["DriveType"].Value.ToString()) == 3) //You can check the drive type here
//{
// sDeviceId will hold the drive name eg "C:"
String sDeviceId = moDrive.Properties["DeviceId"].Value.ToString();
// dSize and dFree will hold the size of the drive and free space in bytes
double dSize = double.Parse(moDrive.Properties["Size"].Value.ToString());
double dFree = double.Parse(moDrive.Properties["FreeSpace"].Value.ToString()); //In Byte
//}
}
catch
{
}
}
mocDrives.Dispose();
mcDriveClass.Dispose();
}Hope it helps!
All the best, Martin
thanks to both of you, seems both solutions would be able to solve my problem! thanks again donovan
rather have something you don't need, than need something you don't have