473,382 Members | 1,389 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,382 software developers and data experts.

USB Storage device

basti42
18
hello!
how would i know if the USB Storage device is inserted
and determine the drive letter?

========
VS 2005
Jul 9 '07 #1
8 2591
Plater
7,872 Expert 4TB
It is possible to do this with the windows message pump.
It's not easy. (I think there might be a WMI event for it, but I don't know of it)

Here's a bit of the code I use for it:

(put this in your form code)
Expand|Select|Wrap|Line Numbers
  1. [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
  2.         protected override void WndProc(ref Message m)
  3.         {
  4.             if (m.Msg == (int)WM_Message.WM_DEVICECHANGE)
  5.             {
  6.                 handleDeviceChange(m);
  7.             }
  8.             else
  9.             {
  10.                 base.WndProc(ref m);
  11.             }
  12.         }
  13.  
  14.  
  15.         private void handleDeviceChange(Message m)
  16.         {
  17.             DateTime now = DateTime.Now; 
  18.             string msg = now.ToString()+" "+  "DeviceChanged: ";
  19.             switch ((DBT_DeviceTypes)m.WParam)
  20.             {
  21.                 case DBT_DeviceTypes.DBT_DEVICEARRIVAL:
  22.                     msg += "   DEVICE ARRIVAL \r\n";
  23.                     break;
  24.                 case DBT_DeviceTypes.DBT_DEVICEREMOVECOMPLETE:
  25.                     msg += "   DEVICE REMOVE COMPLETE \r\n";
  26.                     break;
  27.                 case DBT_DeviceTypes.DBT_DEVNODES_CHANGED:
  28.                     msg += "   DEVICE NODES CHANGED \r\n";
  29.                     break;
  30.                 default:
  31.                     msg += "0x" + m.WParam.ToString("X4") + " \r\n";  
  32.                     break;
  33.             }
  34.             if ((int)m.LParam != 0)
  35.             {
  36.                 _DEV_BROADCAST_HANDLE dbh = (_DEV_BROADCAST_HANDLE)m.GetLParam(typeof(_DEV_BROADCAST_HANDLE));
  37.                 _DEV_BROADCAST_VOLUME dbv = (_DEV_BROADCAST_VOLUME)m.GetLParam(typeof(_DEV_BROADCAST_VOLUME));
  38.                 _DEV_BROADCAST_HDR db = (_DEV_BROADCAST_HDR)m.GetLParam(typeof(_DEV_BROADCAST_HDR));
  39.  
  40.                 string drive = "   Drive: " + Messages.FirstDriveFromMask((UInt32)dbv.dbcv_unitmask) + ": \r\n";
  41.                 string flags = "   Flags: 0x" + dbv.dbcv_flags.ToString("X4")+" \r\n";
  42.  
  43.                 string devicetype = "   Device Type: 0x" + dbh.dbch_devicetype.ToString("X4")+"\r\n";
  44.                 string packagesize = "   Size: 0x" + dbh.dbch_size.ToString("X4") + "\r\n";
  45.                 string eventid = "   EventGUID: 0x"+dbh.dbch_eventguid.ToString("X4")+"\r\n";
  46.                 string data = "   Data: 0x"+dbh.dbch_data.ToString("X2") + "\r\n";
  47.  
  48.                 msg += drive;
  49.                 msg += flags;
  50.                 msg += devicetype;
  51.                 msg += packagesize;
  52.                 msg += eventid;
  53.                 msg += data;
  54.  
  55.                 //tb1.AppendText(m.ToString() + "\r\n");
  56.  
  57.             }
  58.             //do something with msg if you want
  59.         }
  60.  
You will need to get your hands on the ENUMs I used as well as what goes in those structs (they are all defined at msdn)
Jul 9 '07 #2
TRScheel
638 Expert 512MB
If you arent worried about performance, this will be helpful:

Expand|Select|Wrap|Line Numbers
  1. private static bool Continue = true;
  2.  
  3. [STAThread]
  4. static void Main(string[] args)
  5. {
  6.     Thread thread = new Thread(DriveChecker);
  7.     thread.Start();
  8.     while (Continue)
  9.     {
  10.         if (Console.ReadLine() == "end")
  11.             Continue = false;
  12.     }
  13. }
  14.  
  15. static void DriveChecker()
  16. {
  17.     List<string> currentDrives = new List<string>();
  18.     while (Continue)
  19.     {
  20.  
  21.         List<string> updatedDrives = new List<string>();
  22.         for (int i = 100; i < 123; i++)
  23.         {
  24.             DirectoryInfo di = new DirectoryInfo(string.Format("{0}:\\", System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { (byte)i })));
  25.             if (di.Exists)
  26.                 updatedDrives.Add(System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { (byte)i }));
  27.         }
  28.  
  29.         updatedDrives.ForEach(delegate(string Drive) { if (!currentDrives.Contains(Drive)) { Console.WriteLine("Found New Drive: {0}", Drive); } });
  30.         currentDrives.ForEach(delegate(string Drive) { if (!updatedDrives.Contains(Drive)) { Console.WriteLine("Lost Drive: {0}", Drive); } });
  31.  
  32.         currentDrives = updatedDrives;
  33.  
  34.         Thread.Sleep(100);   
  35.     }
  36. }
  37.  
Increase the sleep time to increase performance. Type end to stop the console program.

EDIT:

100 - 123 are letters D - Z. We dont check A, B, or C.
Jul 9 '07 #3
Plater
7,872 Expert 4TB
Hehe, that is much more simple then mine.
Although mine will tell you if a CD/Disk has been inserted too.
Jul 9 '07 #4
TRScheel
638 Expert 512MB
Hehe, that is much more simple then mine.
Although mine will tell you if a CD/Disk has been inserted too.
Actually, it will catch the CD rom insertion... it just cant tell the difference between a CD and a USB stick.
Jul 9 '07 #5
basti42
18
tnx plater but there's an error

1. WM_Message
error: The name 'WM_Message' does not exist in the current context

2.
_DEV_BROADCAST_HANDLE dbh = (_DEV_BROADCAST_HANDLE)m.GetLParam(typeof(_DEV_BRO ADCAST_HANDLE));
_DEV_BROADCAST_VOLUME dbv = (_DEV_BROADCAST_VOLUME)m.GetLParam(typeof(_DEV_BRO ADCAST_VOLUME));
_DEV_BROADCAST_HDR db = (_DEV_BROADCAST_HDR)m.GetLParam(typeof(_DEV_BROADC AST_HDR));
error: Invalid expression term ')'

pls. help.
Jul 10 '07 #6
basti42
18
hello there! disregard my previous reply

i think i found what im lookin for, my next step now is how could i search all files including subfolders in the USB storage since it is obvious that i already know the drive letter?

ex: e:\*.* and e:\foldername1\foldername2

tnx in advance
Jul 10 '07 #7
Plater
7,872 Expert 4TB
Yeah, I didn't give you all the enums and structures because they're a few pages long, but they're easy to find online.

tnx plater but there's an error

1. WM_Message
error: The name 'WM_Message' does not exist in the current context
As for directory searching, look at the System.IO.Directory and System.IO.DirectoryInfo objects (they have a getfiles() and similar usefull functions)
Jul 10 '07 #8
basti42
18
i found this on microsoft's website

void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, txtFile.Text))
{
lstFilesFound.Items.Add(f);
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}

tnx plater!
Jul 11 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kevin Altis | last post by:
Does anyone have experience running Python from a USB storage device? Whether it is the need for doing a demo of your application, doing a bit of consulting, or just showing off Python, it would be...
5
by: Ben Finney | last post by:
Howdy all, I'm experimenting with carrying my personal computing environment around on a keychain USB flash storage device. I have the usual suspects on there: SSH keys, GPG keys, program...
5
by: Bernhard Krautschneider | last post by:
hello group, is it possible to do a storage snapshot of a running ms-sql database without losing transactions? What tasks must be done before such a snapshot. thanks in advance, Bernhard
5
by: Graham Stevenson | last post by:
Hi, I have a USB mass storage device being developed by colleagues, and need to interface that to a Windows Forms application (C#). The device is neither purely a storage device nor an HID...
0
by: Geoff | last post by:
I am looking for a Win 2003 version of the Microsoft Storage Device Registry Cleaner Scrubber Tool. I have the one for Windows 2000 but need one that works for 2003. This is to clean the registry...
1
by: John Rauhe | last post by:
Hello, Does anybody know how to detect if an mass-storage device has been added to the system ? I am making a program that will (should) detect when a CompactFlash memory card has been inserted...
0
by: Zorba.GR | last post by:
IBM DB2 Connect Enterprise Edition v8.2, other IBM DB2 (32 bit, 64 bit) (MULTiOS, Windows, Linux, Solaris), IBM iSoft Commerce Suite Server Enterprise v3.2.01, IBM Tivoli Storage Resource Manager...
0
by: Ivan | last post by:
Hi, all, How to get BusRelations of a USB Mass Storage Device in C#? Or, do we have any function to get BusRelations of a USB Mass Storage Device, even not in C#? Any information is...
3
by: Max | last post by:
Not sure exactly how to describe this or if it's even possible, so I'll do my best and would appreciate some suggestions. I'm currently working on a service that will allow me to automate some...
0
by: zhensoftware | last post by:
USB storage devices have gained popularity. It can be host to viruses, Trojans, hacker toolkits, worms or other forms of malicious programs. For example, when you plug your USB disk into a computer...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.