Results 1 to 23 of 23

Thread: Display Drive Names?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Display Drive Names?

    Well hi! I found sume code in VB6- but converted it to VB.NET 2010. The program puts all of your drives into a listbox but its only the letter name, how can i get the drive name too? Here is my code:

    Code:
    Public Class Form1
        Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Integer, ByVal lpBuffer As String) As Integer
        Private Function GetDriveStrings() As String
            Dim result As Integer
            Dim strDrives As String
            Dim lenStrDrives As Integer
            result = GetLogicalDriveStrings(0, strDrives)
            strDrives = New String(Chr(0), result)
            lenStrDrives = result
            result = GetLogicalDriveStrings(lenStrDrives, strDrives)
            If result = 0 Then
                GetDriveStrings = ""
            Else
                GetDriveStrings = strDrives
            End If
        End Function
    
        Private Sub DisplayDriveTypes(ByRef drives As String)
            Dim pos As Integer
            Dim drive As String
            List1.Items.Clear()
            pos = 1
            Do While Not Mid(drives, pos, 1) = Chr(0)
                drive = Mid(drives, pos, 3)
                pos = pos + 4
                List1.Items.Add("(" & UCase(drive) & ")")
            Loop
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strDrives As String
            strDrives = GetDriveStrings()
    
            If strDrives = "" Then
                MsgBox("No Drives were found!", MsgBoxStyle.Critical)
            Else
                DisplayDriveTypes(strDrives)
            End If
        End Sub
    End Class

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

    Re: Display Drive Names?

    You don't need any of that API stuff. Use the System.IO.DriveInfo class in VB.NET.
    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
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    o ok....

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    umm how would i add all the drives to the listbox?

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display Drive Names?

    like this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     ListBox1.Items.Clear()
    3.     For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
    4.         ListBox1.Items.Add(drive.Name)
    5.     Next
    6. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    how would i make it say Local Disk C:/ instead of just C:/

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Display Drive Names?

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     ListBox1.Items.Clear()
    3.     For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
    4.         Dim itemText As String = drive.Name
    5.         If drive.IsReady AndAlso drive.VolumeLabel <> "" Then
    6.             itemText = drive.VolumeLabel
    7.         Else
    8.             Select Case drive.DriveType
    9.                 Case DriveType.Fixed : itemText = "Local Disk "
    10.                 Case DriveType.CDRom : itemText = "CD-ROM "
    11.                 Case DriveType.Network : itemText = "Network Drive "
    12.                 Case DriveType.Removable : itemText = "Removable Disk "
    13.                 Case DriveType.Unknown : itemText = "Unknown "
    14.             End Select
    15.         End If
    16.         itemText &= drive.Name
    17.         ListBox1.Items.Add(itemText)
    18.     Next
    19. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    ok...i got stuck again im trying to add the items to a listview as seprate colums i have the drive name right but for the drive letter cloum they add the first on to the drive letter colum and no more?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    nvm i fixed it, I accedentally put the Dim Count as Integer = 0 inside the for-next statement... ok now i am wondering if its possible to make it so when double click a drive it will open it...?

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Display Drive Names?

    Hey,

    Have a look at the Process Class:

    http://msdn.microsoft.com/en-us/libr...s.process.aspx

    In the click event that you are using, use Process.Start and pass in the drive that you want to open, assuming of course you are talking about opening Windows Explorer.

    Gary

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    so wat like process.start(c:/) or suthing?

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Display Drive Names?

    Hey,

    Yip, something like that, although, there are no overloaded methods for the Process.Start method that take the parameter that you have given it:

    http://msdn.microsoft.com/en-us/libr...ess.start.aspx

    Most likely, you would use this:

    http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

    Gary

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    will that work with just c:/ ?

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    ok..it works but how do i make it so when double click an item in the listview it will go to that item, like it will open the drive letter under the Drive letter Column?

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

    Re: Display Drive Names?

    Quote Originally Posted by reconrey View Post
    ok..it works but how do i make it so when double click an item in the listview it will go to that item, like it will open the drive letter under the Drive letter Column?
    In the ListView's DoubleCilck event, parse the column that has the drive letter and use that as a parameter for Process.Start.
    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"....

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    so DvLt.ListView.SelectedItems.ToString?

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    ummm...its not working...

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

    Re: Display Drive Names?

    Quote Originally Posted by reconrey View Post
    ummm...its not working...
    What's not working? Instead of waiting for us to write the code for you, show what you've done so we can help you fix it for yourself. That is the best way to learn.
    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

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    ok....i have put this under the listview double click event(DvLt is the column i want it to navigate to...):

    Code:
    Dim returnValue As Process
            For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
                Try
                    Dim Ltr As String = DvLt.ListView.SelectedItems.ToString
                    returnValue = Process.Start(DvLt.ListView.SelectedItems.ToString)
                Catch ex As Exception
    
                    MsgBox(ex.Message, MsgBoxStyle.Critical, "ERROR")
                End Try
    
            Next

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    nvm i figured it out

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

    Re: Display Drive Names?

    Quote Originally Posted by reconrey
    ok....i have put this under the listview double click event(DvLt is the column i want it to navigate to...):
    You're making this harder than it needs to be. You don't have to do anything with populating new drives.

    If all you're trying to do is launch the drive when you double click the item, all you have to do is set a variable as string with the ListView Selected Item's Text. If the drive letter is in the first column, use the first selected item (0), and (1) if it's in the second and so on.

    From there, call Process.Start and pass that variable as an argument.

    It's literally 3 lines of code.
    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"....

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Feb 2010
    Posts
    106

    Re: Display Drive Names?

    i know...i fixed it less code now! I WIN!

  23. #23
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Display Drive Names?

    Hey,

    For the benefit of other people within this community, you might want to think about posting the code that you ended up using.

    Also, remember to mark your thread as resolved.

    Gary

Tags for this Thread

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