![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Drive Insertion Detection
Hello everyone, It's been a couple of weeks I believe.
I have a question to ask about detecting when a disk is inserted. a disk being either a floppy, CD, USB etc.. As you now windows automatically detects a Drive or Disk when inserted, and unless the option is disabled, it will prompt you with a dialog giving you options of what to do / how you want to open the data. I am writing a program that needs the ability to know when a new drive letter is mounted. I have looked around the internet for a couple of days regarding this matter, and other than trying my damned hardest to re-write some C# code I have had no success. maybe somebody here has an idea? Info : My program wants to be notified when there is a USB drive inserted and on the occasion of it being inserted the program will search the drive for a specific file, if the file exists it can carry out its duty, otherwise nothing is done. I must thank anyone who even takes time to look at this thread.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Tricky. Depending on how you want to do it, there would be a few ways. Which is most effective, I don't know. In the old VB6, I would have used a timer probably, as I don't remember there being a control for it. But, with VB.NET, I figured there would be some control to help you with ths process. I've just been looking at the controls, and I can only see one that might help you. Check out the FileSystenWatcher (under components). I don't know how well it performs, but it seems to be the closeest thing to what you want, without actually coding a lot yourself. Hope it helps
![]() |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Well yes, as you mention there would be a few way's I would imagine. Thanks for bringing the FileSystemWatcher to my attention, as it happens I had started coding a routine to handle this but it is very 'Matchsticky' and 'Glued Together'.
Incase your curious Booooze I shall explain just a little. I used a DriveListBox with a timer that refreshes every 2 seconds... Then a ComboBox that grabs the number of drives in the DriveListBox also using a timer ticking slightly after the first... The third item is a NumericUpDown that grabs the number contained in the ComboBox with a timer that ticks just under a second after timer number 2 If the NumericUpDown Value is ever smaller than the ComboBox Value then it searches the drives listed for the file I am looking for. I know this is a real cowboy job but i was just playing around last night with a method of how to do this, I know there will be a lot of critisism for this but heyyy I can work with critisism. Thanks for your info Booooze, I shall see how it pans out tonight with a FileSystemWatcher. ![]()
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#4 |
|
Expert Programmer
|
yeah, I was going to say that you could just use timers and keep checking for the new drive, but as you said, it's liked glued together:p Although, If you would like to make your code that you are making a little more effiecient, I would suggest using the Directory class. e.g:
something = Directory.GetLogicalDrives If you want more code, here is some code I developed in C# for a guy who needed it and he converted it VB.NET. Perhaps it might be of some use to you. It gets the Names and Volume labels of the drives. Dim drives As String() = Directory.GetLogicalDrives
For Each drive As String In drives
Dim di As DriveInfo = New DriveInfo(drive)
Dim newlst1 As ListViewItem
newlst1 = lstdrives.Items.Add(0)
Try
newlst1.Text = (drive + " (" + di.VolumeLabel + ")")
Catch ex As Exception
newlst1.Text = (drive & "")
End Try
NextAnyways, good luck ![]() |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Thank you for your time Booooze.
Tomorow I shall tell you how I get on tonight if you wish.
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2006
Location: Fayettehell, NC
Posts: 56
Rep Power: 3
![]() |
I think Booooze has a good idea going here. Just do an if loop to check for any drives there weren't there when the app started. Then do a file look up via the system.io.directory methods. Anyhow, i'm interested on how it works out.
![]()
__________________
_Marshall_ "America has bred a society that is innocent and incapable of accepting responsibility, but yet, is able to place blame on others without guilt." |
|
|
|
|
|
#7 |
|
Professional Programmer
|
Yeha I'm the guy booze done that for. What I'd do is use DriveInfo I think it goes soemthign like this
Imports System.IO Public Class Form1 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Timer1.Tick Dim di As DriveInfo = DriveInfo.GetDrives If di.IsReady Then (yourcodehere) End if End Sub End Class What that will do, is check if your drive is ready. It will automaticlaly do it for HDD's because they are always ready. But Like Floppy drives and CD drives arent ready unless something is in them. But thats what I'd use I'm sure there is other ways.
__________________
Forgiveness is the fragrance that the violet sheds on the heal that has crushed it. - Mark Twain Destruction leads to a very rough road, but it also breeds creation. |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
thanks for all your input on this matter and Bigguy I may try it your way later so that it only looks for removables, as that's my main idea. However I did write my code for this procedure over the weekend, and it works quite well.
I should of thought on and brought it with me this morning, but alas. I will post it tomorow though if anyone is at all interested in seeing it. ![]()
__________________
Mona Lisa must of had the highway blues you can tell by the way she smiles.. |
|
|
|
|
|
#9 |
|
Expert Programmer
|
I want to see it
Trying to collect all the code I can and save it, so I can give it to other people, just like I did for you Saves me having to google stuff constantly |
|
|
|
|
|
#10 |
|
Professional Programmer
|
I'd like also, please.
__________________
Forgiveness is the fragrance that the violet sheds on the heal that has crushed it. - Mark Twain Destruction leads to a very rough road, but it also breeds creation. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Slackware installation guide for Linux beginners | coldDeath | Coder's Corner Lounge | 104 | Jul 29th, 2007 5:40 AM |
| Stupid Windows, stupid DVD-RW drive | Prm753 | Coder's Corner Lounge | 9 | Jan 21st, 2006 8:26 PM |
| Linked list insertion at the tail | elford | Java | 9 | Jan 7th, 2006 10:04 AM |
| Second Hard Drive Issues... | ViOLATiON | Coder's Corner Lounge | 2 | Dec 31st, 2005 1:45 AM |
| minimizing hard drive activity? | chepfaust | Coder's Corner Lounge | 3 | Mar 25th, 2005 11:49 PM |