|
-
Feb 20th, 2010, 01:11 AM
#1
Thread Starter
Lively Member
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
-
Feb 20th, 2010, 01:40 AM
#2
Re: Display Drive Names?
You don't need any of that API stuff. Use the System.IO.DriveInfo class in VB.NET.
-
Feb 20th, 2010, 01:48 AM
#3
Thread Starter
Lively Member
-
Feb 20th, 2010, 01:52 AM
#4
Thread Starter
Lively Member
Re: Display Drive Names?
umm how would i add all the drives to the listbox?
-
Feb 20th, 2010, 02:49 AM
#5
Re: Display Drive Names?
like this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListBox1.Items.Clear() For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives ListBox1.Items.Add(drive.Name) Next End Sub
-
Feb 20th, 2010, 03:29 AM
#6
Thread Starter
Lively Member
Re: Display Drive Names?
how would i make it say Local Disk C:/ instead of just C:/
-
Feb 20th, 2010, 03:51 AM
#7
Re: Display Drive Names?
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListBox1.Items.Clear() For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives Dim itemText As String = drive.Name If drive.IsReady AndAlso drive.VolumeLabel <> "" Then itemText = drive.VolumeLabel Else Select Case drive.DriveType Case DriveType.Fixed : itemText = "Local Disk " Case DriveType.CDRom : itemText = "CD-ROM " Case DriveType.Network : itemText = "Network Drive " Case DriveType.Removable : itemText = "Removable Disk " Case DriveType.Unknown : itemText = "Unknown " End Select End If itemText &= drive.Name ListBox1.Items.Add(itemText) Next End Sub
-
Feb 20th, 2010, 12:16 PM
#8
Thread Starter
Lively Member
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?
-
Feb 20th, 2010, 12:34 PM
#9
Thread Starter
Lively Member
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...?
-
Feb 20th, 2010, 12:37 PM
#10
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
-
Feb 20th, 2010, 01:18 PM
#11
Thread Starter
Lively Member
Re: Display Drive Names?
so wat like process.start(c:/) or suthing?
-
Feb 20th, 2010, 01:25 PM
#12
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
-
Feb 20th, 2010, 01:43 PM
#13
Thread Starter
Lively Member
Re: Display Drive Names?
will that work with just c:/ ?
-
Feb 20th, 2010, 04:24 PM
#14
Thread Starter
Lively Member
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?
-
Feb 20th, 2010, 04:49 PM
#15
Re: Display Drive Names?
 Originally Posted by reconrey
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 20th, 2010, 05:48 PM
#16
Thread Starter
Lively Member
Re: Display Drive Names?
so DvLt.ListView.SelectedItems.ToString?
-
Feb 21st, 2010, 01:16 PM
#17
Thread Starter
Lively Member
Re: Display Drive Names?
ummm...its not working...
-
Feb 21st, 2010, 04:58 PM
#18
Re: Display Drive Names?
 Originally Posted by reconrey
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.
-
Feb 21st, 2010, 05:57 PM
#19
Thread Starter
Lively Member
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
-
Feb 21st, 2010, 06:19 PM
#20
Thread Starter
Lively Member
Re: Display Drive Names?
nvm i figured it out
-
Feb 21st, 2010, 06:22 PM
#21
Re: Display Drive Names?
 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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 21st, 2010, 07:42 PM
#22
Thread Starter
Lively Member
Re: Display Drive Names?
i know...i fixed it less code now! I WIN!
-
Feb 22nd, 2010, 03:48 AM
#23
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|