|
-
May 23rd, 2009, 09:54 PM
#1
Thread Starter
Member
Optical/Hard Drives Control
I want to make 2 dynamic comboboxes; The first will list all the hard-drives/thumb-drives etc, the second will list all the CD/DVD drives.
Beneath each combobox there will be a set of buttons that control the selected drive:
Beneath the hard-drive combobox there will be two buttons: one is "open" (will open the select drive in explorer) the other is "remove" (checks if the selected drive is removable and if so safely removes the drive).
Beneath the CD/DVD combobox there will also be two buttons: one is "open" (will open the drive's contents, if present, in explorer), the other is "autorun" (will execute the autorun for the disc).
Here's an image to show you what I'm talking about (Ignore the open/close tray buttons - I managed to make them work):

I know I'm asking for a lot, but if anyone can help me with this I'll be eternally grateful
-
May 23rd, 2009, 10:15 PM
#2
Re: Optical/Hard Drives Control
You should be able to use DriveInfo.GetDrives to get all the drives. You can use LINQ or a loop to get only the correct drive types. For the rest I think you'd need to use WMI. Gigemboy has submitted a WMI thread in the VB.NET CodeBank thread.
-
May 24th, 2009, 05:59 PM
#3
Thread Starter
Member
Re: Optical/Hard Drives Control
 Originally Posted by jmcilhinney
You should be able to use DriveInfo.GetDrives to get all the drives. You can use LINQ or a loop to get only the correct drive types. For the rest I think you'd need to use WMI. Gigemboy has submitted a WMI thread in the VB.NET CodeBank thread.
Alright, let's take it easy. First I tried using The DriveInfo method like this:
Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
DriveInfo.GetDrives()
End Sub
It says that DriveInfo is not declared.
Second, I have no idea what "LINQ" is, or how to set a loop.
Finally, what's a WMI?
-
May 24th, 2009, 06:06 PM
#4
Re: Optical/Hard Drives Control
1) DriveInfo is part of the IO namespace which is not initially imported. Use IO.DriveInfo.GetDrives() or put Imports System.IO at the top of your code:
vb.net Code:
For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives() MessageBox.Show(Drive.Name) Next
2) LINQ in a nutshell is basically a set of extensions to the .NET language. Information here:
http://msdn.microsoft.com/en-us/netf.../aa904594.aspx
3) WMI or Windows Management Instrumentation info can be found here:
http://msdn.microsoft.com/en-us/libr...42(VS.85).aspx
And here is thread jmc referenced:
http://www.vbforums.com/showthread.php?t=387626
-
May 24th, 2009, 06:07 PM
#5
Re: Optical/Hard Drives Control
The following code is from a post on D.I.C.:
vb.net Code:
Private Sub HardDrives() Dim allDrives() As DriveInfo = DriveInfo.GetDrives() Dim d As DriveInfo For Each d In allDrives lstdrives.Items.Add(d.Name) lstdrives.Items.Add(d.DriveType.ToString) If d.IsReady = True Then lstdrives.Items.Add(d.VolumeLabel) lstdrives.Items.Add(d.DriveFormat) lstdrives.Items.Add(" Available space to current user: " + d.AvailableFreeSpace.ToString + " " + " bytes") lstdrives.Items.Add(" Total available space: " + d.TotalFreeSpace.ToString + " " + " bytes") lstdrives.Items.Add(" Total size of drive: " + d.TotalSize.ToString + " " + "bytes") End If Next End Sub
This code assumes the name of your container is "lstdrives". You can change it to whatever you need to. If I'm not mistaken, you'll need to import System.IO namespace.
Second, I recommend reading up on DriveInfo.GetDrives on MSDN
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
May 24th, 2009, 06:20 PM
#6
Thread Starter
Member
Re: Optical/Hard Drives Control
Ok, so to list all the drives I tried this:
Code:
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives()
ComboBox2.Text(Drive.Name)
Next
End Sub
It failes in the ComboBox2.Text area and I have to click a button to see the drives. I'm sorry for asking what may appear as obvious to you, but I have no idea how to get just the hard drives to show in the left combobox, and just the cd drives to show on the right.
-
May 24th, 2009, 06:22 PM
#7
Re: Optical/Hard Drives Control
vb.net Code:
ComboBox2.Items.Add(Drive.Name)
There's really no need to apologize for something you don't know.
-
May 24th, 2009, 06:27 PM
#8
Thread Starter
Member
Re: Optical/Hard Drives Control
Thanks for your patience.
Ok, so I managed to make the list load on form load. However, the list is empty and the user can't see that there's drives in the combobox, only if he clicks it. How do I make one of the drives (like c:\) appear as the default? And how to I filter the cd drives from the hard drives?
Last edited by smith01; May 24th, 2009 at 06:34 PM.
-
May 24th, 2009, 06:37 PM
#9
Re: Optical/Hard Drives Control
You will need to change the ComboBox.SelectedIndex or ComboBox.SelectedItem property like so:
This assumes there is at least item in your combobox:
vb.net Code:
Me.ComboBox1.SelectedIndex = 0
This assumes there is an item called "C:\" in your combobox items:
vb.net Code:
Me.ComboBox1.SelectedItem = "C:\"
As for the filter separation.
The DriveInfo class has an enum called DriveType. So in that For Each loop, you can check the Drive.DriveType and code accordingly:
vb.net Code:
For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives() If Drive.DriveType = DriveType.CDRom Then Me.ComboBox1.Items.Add(Drive.Name) Else Me.ComboBox2.Items.Add(Drive.Name) End If Next
Last edited by ForumAccount; May 24th, 2009 at 06:41 PM.
-
May 24th, 2009, 06:43 PM
#10
Re: Optical/Hard Drives Control
For future reference, the MSDN documentation for every type tells you what assembly it's declared in and what namespace it's a member of. If you ever get told that a type isn't defined again you simply select Help -> Index from the main menu, enter the type name and open the overview topic. You can then make sure that your project references the required assembly and imports the required namespace, or else you can qualify the type name with its namespace.
As for terms like LINQ and WMI, I suggest that you look first and ask questions later. Googling could have told you what they were.
I'm not saying that you shouldn't ask questions here but you should do what you can for yourself first, then ask about what you can't find or don't understand.
As for the most recent question, you can load the drive list when you click a button because you're handling the button's Click event. If you want to load the drive list when the form loads then you need to handle the form's Load event. Just as you double-click a button to create a Click event handler, you double-click a form to create a Load event handler. Double-clicking any component in the designer will create a handler for its default event, which varies from component to component. There are several other ways to create event handlers too.
-
May 24th, 2009, 06:44 PM
#11
Thread Starter
Member
Re: Optical/Hard Drives Control
 Originally Posted by ForumAccount
You will need to change the ComboBox.SelectedIndex or ComboBox.SelectedItem property like so:
As for the filter separation.
The DriveInfo class has an enum called DriveType. So in that For Each loop, you can check the Drive.DriveType and code accordingly:
vb.net Code:
For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives() If Drive.DriveType = DriveType.CDRom Then Me.ComboBox1.Items.Add(Drive.Name) Else Me.ComboBox2.Items.Add(Drive.Name) End If Next
OK, when I do this it tells me that on this line:
Code:
If Drive.DriveType = DriveType.CDRom Then
the second DriveType is not declared.
Last edited by smith01; May 24th, 2009 at 06:50 PM.
-
May 24th, 2009, 06:55 PM
#12
Re: Optical/Hard Drives Control
Just like DriveInfo, DriveType is a member of the System.IO namespace. You need to either import that namespace or else qualify the name of every member, as you did with DriveInfo.
-
May 24th, 2009, 06:57 PM
#13
Re: Optical/Hard Drives Control
You should also note that, if you'd typed that code out rather than pasting, Intellsense would have helped you out. As soon as you typed the '=' on that line Intellisense would have popped up a list of options and they would already have been qualified with the namespace. Try deleting that line back to the '=' and type it in again and you'll see.
-
May 24th, 2009, 06:59 PM
#14
Thread Starter
Member
Re: Optical/Hard Drives Control
Ok, I got it to filter like so:
vb.net Code:
'Lists all the hard drives in the LEFT combobox, all the cd-roms in the RIGHT For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives() If Drive.DriveType = Drive.DriveType.CDRom Then Me.ComboBox1.Items.Add(Drive.Name) Me.ComboBox1.SelectedIndex = 0 Else Me.ComboBox2.Items.Add(Drive.Name) Me.ComboBox2.SelectedIndex = 0 End If Next
Only now this line (Drive.DriveType.CDRom) has a green underline to it that says: "Access of shared member, constant members... etc"
Last edited by smith01; May 24th, 2009 at 07:24 PM.
-
May 24th, 2009, 07:33 PM
#15
Re: Optical/Hard Drives Control
Go back and read my last two posts. They tell you what you should be doing.
-
May 24th, 2009, 07:38 PM
#16
Thread Starter
Member
Re: Optical/Hard Drives Control
 Originally Posted by jmcilhinney
Go back and read my last two posts. They tell you what you should be doing.
I did. I still don't get it.
-
May 24th, 2009, 07:40 PM
#17
Re: Optical/Hard Drives Control
Type in the IDE 'If Drive.DriveType ='
Once you do that, a list will come up of values you can use there, pick the CDRom one.
-
May 24th, 2009, 07:49 PM
#18
Thread Starter
Member
Re: Optical/Hard Drives Control
 Originally Posted by ForumAccount
Type in the IDE 'If Drive.DriveType ='
Once you do that, a list will come up of values you can use there, pick the CDRom one.
OK, I got it, thanks. Do you also know how I can make the drives' full name appear? (I don't mean the volumelabel or the drive letter, i'm talking about the drives' name like LG-DVD-ETC etc).
-
May 24th, 2009, 07:52 PM
#19
Re: Optical/Hard Drives Control
 Originally Posted by smith01
OK, I got it, thanks. Do you also know how I can make the drives' full name appear? (I don't mean the volumelabel or the drive letter, i'm talking about the drives' name like LG-DVD-ETC etc).
I think you'll find that that would require WMI.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|