We help IT Professionals succeed at work.

Get drive letter based on volume name VB .NET

Hi all
i need to code to search all hard drives attached for the volume label named Test123 if test123 then get drive letter which will i will then use in anothe part of the code
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You could do something like:
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim name As String = "Test123"
        Dim drive As String = GetDriveLetter(name)
        If Not IsNothing(drive) Then
            MessageBox.Show(name & " --> " & drive)
        End If
    End Sub

    Private Function GetDriveLetter(ByVal Volume As String) As String
        For Each drive As DriveInfo In My.Computer.FileSystem.Drives
            If drive.DriveType = DriveType.Fixed AndAlso drive.VolumeLabel.ToLower = Volume.ToLower Then
                Return drive.RootDirectory.Name
            End If
        Next
        Return Nothing
    End Function

End Class

Open in new window

Author

Commented:
i am getting this error

The volume does not contain a recognized file system. Please make sure that all required file system drivers are loaded and that the volume is not corrupted.

Author

Commented:
sorry for got breaks here
If drive.DriveType = DriveType.Fixed AndAlso drive.VolumeLabel.ToLower = Volume.ToLower Then

Author

Commented:
well it works if the Drive C is named Test123 but if i name my drive E it wont

Author

Commented:
it also works if the second partition is on the same physical HDD .
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
I only have one drive with a giant partition so I can't test out different scenarios...sorry.  =\

Not sure if you can trap errors like this:

    Private Function GetDriveLetter(ByVal Volume As String) As String
        For i As Integer = 0 To My.Computer.FileSystem.Drives.Count - 1
            Try
                If My.Computer.FileSystem.Drives(i).DriveType = DriveType.Fixed AndAlso My.Computer.FileSystem.Drives(i).VolumeLabel.ToLower = Volume.ToLower Then
                    Return My.Computer.FileSystem.Drives(i).RootDirectory.Name
                End If
            Catch ex As Exception
            End Try
        Next
        Return Nothing
    End Function

Author

Commented:
ha your a genious below is the final code, i made it so it launces the windows 7 system image creator
        Dim name As String = "Test123"
        Dim drive As String = GetDriveLetter(name)
        Dim sdrive
        If Not IsNothing(drive) Then
            sdrive = (drive.Remove(2, 1))
            'MsgBox(sdrive)
            Shell("wbadmin start backup -backuptarget:" & sdrive & " " & " -include:c: -quiet")

        End If
    End Sub


    Private Function GetDriveLetter(ByVal Volume As String) As String
        For i As Integer = 0 To My.Computer.FileSystem.Drives.Count - 1
            Try
                If My.Computer.FileSystem.Drives(i).DriveType = DriveType.Fixed AndAlso My.Computer.FileSystem.Drives(i).VolumeLabel.ToLower = Volume.ToLower Then
                    Return My.Computer.FileSystem.Drives(i).RootDirectory.Name
                End If
            Catch ex As Exception
            End Try
        Next
        Return Nothing
    End Function

Open in new window

Author

Commented:
Thnks a Mil :)