Results 1 to 19 of 19

Thread: Optical/Hard Drives Control

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    Re: Optical/Hard Drives Control

    Quote Originally Posted by jmcilhinney View Post
    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?

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives()
    2.             MessageBox.Show(Drive.Name)
    3.         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

  5. #5
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Optical/Hard Drives Control

    The following code is from a post on D.I.C.:
    vb.net Code:
    1. Private Sub HardDrives()
    2.          
    3.                 Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
    4.  
    5.                 Dim d As DriveInfo
    6.                 For Each d In allDrives
    7.  
    8.                         lstdrives.Items.Add(d.Name)
    9.  
    10.                         lstdrives.Items.Add(d.DriveType.ToString)
    11.                         If d.IsReady = True Then
    12.                                 lstdrives.Items.Add(d.VolumeLabel)
    13.                                 lstdrives.Items.Add(d.DriveFormat)
    14.                                 lstdrives.Items.Add("  Available space to current user: " + d.AvailableFreeSpace.ToString + " " + " bytes")
    15.  
    16.                                 lstdrives.Items.Add("  Total available space: " + d.TotalFreeSpace.ToString + " " + " bytes")
    17.  
    18.                                 lstdrives.Items.Add("  Total size of drive: " + d.TotalSize.ToString + " " + "bytes")
    19.                         End If
    20.                 Next
    21.  
    22.  
    23.  
    24.  
    25.         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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    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.

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Optical/Hard Drives Control

    vb.net Code:
    1. ComboBox2.Items.Add(Drive.Name)

    There's really no need to apologize for something you don't know.

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    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.

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. Me.ComboBox1.SelectedIndex = 0

    This assumes there is an item called "C:\" in your combobox items:

    vb.net Code:
    1. 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:
    1. For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives()
    2.             If Drive.DriveType = DriveType.CDRom Then
    3.                 Me.ComboBox1.Items.Add(Drive.Name)
    4.             Else
    5.                 Me.ComboBox2.Items.Add(Drive.Name)
    6.             End If
    7.         Next
    Last edited by ForumAccount; May 24th, 2009 at 06:41 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    Re: Optical/Hard Drives Control

    Quote Originally Posted by ForumAccount View Post
    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:
    1. For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives()
    2.             If Drive.DriveType = DriveType.CDRom Then
    3.                 Me.ComboBox1.Items.Add(Drive.Name)
    4.             Else
    5.                 Me.ComboBox2.Items.Add(Drive.Name)
    6.             End If
    7.         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.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    Re: Optical/Hard Drives Control

    Ok, I got it to filter like so:

    vb.net Code:
    1. 'Lists all the hard drives in the LEFT combobox, all the cd-roms in the RIGHT
    2.         For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives()
    3.             If Drive.DriveType = Drive.DriveType.CDRom Then
    4.                 Me.ComboBox1.Items.Add(Drive.Name)
    5.                 Me.ComboBox1.SelectedIndex = 0
    6.             Else
    7.                 Me.ComboBox2.Items.Add(Drive.Name)
    8.                 Me.ComboBox2.SelectedIndex = 0
    9.             End If
    10.         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.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Optical/Hard Drives Control

    Go back and read my last two posts. They tell you what you should be doing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    Re: Optical/Hard Drives Control

    Quote Originally Posted by jmcilhinney View Post
    Go back and read my last two posts. They tell you what you should be doing.
    I did. I still don't get it.

  17. #17
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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.

  18. #18

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    37

    Re: Optical/Hard Drives Control

    Quote Originally Posted by ForumAccount View Post
    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).

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Optical/Hard Drives Control

    Quote Originally Posted by smith01 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width