So i've been working on this little program which is meant to detect internal or external harddrives and show file sizes etc. All that is fine but the problem is that if i take the program to another computer and run it it shows "E:\" in the combobox when it's not supposed to because "E:\" is my External HDD which i connected it to my laptop and the fact that it's just appearing on the computers i run is weird. I haven't connected my external HDD to those computers.

Any Idea?

Here's the code: http://pastebin.com/tmwda6FH

Thanks!

Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Management
Imports Microsoft.VisualBasic.Devices

Public Class Form1
    Enum InfoTypes
        OperatingSystemName
        ProcessorName
        AmountOfMemory
        VideocardName
        VideocardMem
    End Enum
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ToolStripButton2.Enabled = False
        Label16.Left = 10
        Label16.Top = 89
        Timer5.Start()
        Label5.Visible = False
        PictureBox1.Visible = False
        Label16.Visible = True
        ToolStripComboBox1.Text = "Click Here!"
        ToolStripLabel7.Text = ""
        ToolStripLabel9.Text = ""
        ToolStripLabel11.Text = ""
        ToolStripLabel13.Text = ""
        Timer3.Start()
        Label6.Text = ""
        Label9.Text = ""
        Info()
        Label13.Text = ""
        Label14.Text = ""
        Label15.Text = ""
        Timer1.Start()
        Sdriveer()
    End Sub
    'Used to detected if any of the messages are any of these constants values.
    Private Const WM_DEVICECHANGE As Integer = &H219
    Private Const DBT_DEVICEARRIVAL As Integer = &H8000
    Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
    Private Const DBT_DEVTYP_VOLUME As Integer = &H2
    '
    'Get the information about the detected volume.
    Private Structure DEV_BROADCAST_VOLUME
        Dim Dbcv_Size As Integer
        Dim Dbcv_Devicetype As Integer
        Dim Dbcv_Reserved As Integer
        Dim Dbcv_Unitmask As Integer
        Dim Dbcv_Flags As Short
    End Structure
    Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)
        '
        'These are the required subclassing codes for detecting device based removal and arrival.
        '
        If M.Msg = WM_DEVICECHANGE Then
            Select Case M.WParam
                '
                'Check if a device was added.
                Case DBT_DEVICEARRIVAL

                    Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
                    If DevType = DBT_DEVTYP_VOLUME Then
                        Dim Vol As New DEV_BROADCAST_VOLUME
                        Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
                        If Vol.Dbcv_Flags = 0 Then
                            For i As Integer = 0 To 20
                                If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                    Dim Usb As String = Chr(65 + i) + " Drive"
                                    Timer2.Start()
                                    Label15.Text = "A Device was connected: " & Usb.ToString
                                    ToolStripComboBox1.Items.Clear()

                                    Sdriveer()
                                    Me.Refresh()
                                    Exit For
                                End If
                            Next
                        End If
                    End If
                    '
                    'Check if the message was for the removal of a device.
                Case DBT_DEVICEREMOVECOMPLETE

                    Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
                    If DevType = DBT_DEVTYP_VOLUME Then
                        Dim Vol As New DEV_BROADCAST_VOLUME
                        Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
                        If Vol.Dbcv_Flags = 0 Then
                            For i As Integer = 0 To 20
                                If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                    Dim Usb As String = Chr(65 + i) + " Drive"
                                    Timer2.Start()
                                    Label15.Text = "A Device was removed: " & Usb.ToString
                                    ToolStripComboBox1.Items.Clear()

                                    Sdriveer()
                                    Me.Refresh()
                                    Exit For
                                End If
                            Next
                        End If
                    End If
            End Select
        End If
        MyBase.WndProc(M)
    End Sub
    Public Function Sdriveer()
        For Each drive In Environment.GetLogicalDrives
            Dim InfoDrive As DriveInfo = New DriveInfo(drive)
            If InfoDrive.DriveType = DriveType.Removable Or InfoDrive.DriveType = DriveType.Fixed Or InfoDrive.DriveType = DriveType.CDRom Then

                ToolStripComboBox1.Items.Add(drive)

            End If
        Next
    End Function
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If ToolStripLabel17.Text = "Drive" Then
            ToolStripLabel17.Visible = False
        Else
            ToolStripLabel17.Visible = True
            Dim stringToCleanUp As String = ToolStripComboBox1.SelectedItem
            Dim characterToRemove As String = ":" & "\"
            Dim cleanString As String = Replace(stringToCleanUp, characterToRemove, "")
            ToolStripLabel17.Text = cleanString & " Drive"
        End If
    End Sub
    Private Sub ToolStripComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged
        Try
            ToolStripButton2.Enabled = True
            Label5.Visible = True
            PictureBox1.Visible = True
            ProgressBar1.Visible = True
            Label16.Visible = False
            ToolStripComboBox1.Text = ""
            ToolStripLabel17.Text = ToolStripComboBox1.SelectedItem
            Dim cdrive As System.IO.DriveInfo
            cdrive = My.Computer.FileSystem.GetDriveInfo(ToolStripComboBox1.SelectedItem)
            Label4.Text = (Int(Val(cdrive.TotalSize) / 1024 / 1024))
            Label11.Text = (Int(Val(cdrive.AvailableFreeSpace) / 1024 / 1024))
            Label12.Text = CStr(Val(Label4.Text) - Val(Label11.Text))
            Label5.Text = cdrive.VolumeLabel.ToString.Trim

            If Label4.Text >= 10000 Then
                Label4.Text = (Int(Val(cdrive.TotalSize) / 1073741824))
                Label3.Text = Label4.Text & " GB"
            ElseIf Label4.Text <= 10000 Then
                Label4.Text = (Int(Val(cdrive.TotalSize) / 1024 / 1024))
                Label3.Text = Label4.Text & " MB"
            End If

            If Label11.Text >= 10000 Then
                Label11.Text = (Int(Val(cdrive.AvailableFreeSpace) / 1073741824))
                Label10.Text = Label11.Text & " GB"
            ElseIf Label11.Text <= 10000 Then
                Label11.Text = (Int(Val(cdrive.AvailableFreeSpace) / 1024 / 1024))
                Label10.Text = Label11.Text & " MB"
            End If

            If Label12.Text >= 10000 Then
                Label12.Text = CStr(Val(Label4.Text) - Val(Label11.Text))
                Label7.Text = Label12.Text & " GB"
            ElseIf Label12.Text <= 10000 Then
                Label12.Text = CStr(Val(Label4.Text) - Val(Label11.Text))
                Label7.Text = Label12.Text & " MB"
            End If
            Dim cdrive1 As System.IO.DriveInfo
            cdrive1 = My.Computer.FileSystem.GetDriveInfo(ToolStripComboBox1.SelectedItem)
            Label13.Text = "Drive Is A " & cdrive.DriveType.ToString & " Device"

            Dim cdrive2 As System.IO.DriveInfo
            cdrive2 = My.Computer.FileSystem.GetDriveInfo(ToolStripComboBox1.SelectedItem)
            Label14.Text = "Drive Format: " & cdrive.DriveFormat.ToString

            ToolStripLabel9.Text = Label3.Text
            ToolStripLabel11.Text = Label10.Text
            ToolStripLabel13.Text = Label7.Text

            Label2.Text = Math.Round(Label11.Text / Label4.Text * 100, 2)
            Label6.Text = Label2.Text & "% Free Out Of 100%"
            If Label5.Text = "" Then
                Label5.Text = "Local Disk"
            End If

            ProgressBar1.Maximum = Label4.Text
            ProgressBar1.Value = Label12.Text
            Timer1.Start()
            Timer3.Start()
        Catch ex As Exception

        End Try
    End Sub
    Public Sub Info()
        ToolStripLabel7.Text = ""
        ToolStripLabel3.Text = ""
        ToolStripLabel5.Text = ""
        Label1.Text = Environment.UserName
        Label8.Text = My.Computer.Info.OSFullName
        Information.Label16.Text = ((GetInfo(InfoTypes.ProcessorName)))
        Label18.Text = ((GetInfo(InfoTypes.AmountOfMemory)))
        Information.Label19.Text = ((GetInfo(InfoTypes.VideocardName)))
        Label22.Text = ((GetInfo(InfoTypes.VideocardMem)))
    End Sub

    Public Function GetInfo(ByVal InfoType As InfoTypes) As String


        Dim Info As New ComputerInfo : Dim Value, vganame, vgamem, proc As String
        Dim searcher As New Management.ManagementObjectSearcher( _
            "root\CIMV2", "SELECT * FROM Win32_VideoController")
        Dim searcher1 As New Management.ManagementObjectSearcher( _
            "SELECT * FROM Win32_Processor")
        If InfoType = InfoTypes.OperatingSystemName Then
            Value = Info.OSFullName
        ElseIf InfoType = InfoTypes.ProcessorName Then
            For Each queryObject As ManagementObject In searcher1.Get
                proc = queryObject.GetPropertyValue("Name").ToString
            Next
            Value = proc

        ElseIf InfoType = InfoTypes.AmountOfMemory Then
            Value = Math.Round(Int(Val(Info.TotalPhysicalMemory) / 1024 / 1024))

            Timer4.Start()
        ElseIf InfoType = InfoTypes.VideocardName Then
            For Each queryObject As ManagementObject In searcher.Get
                vganame = queryObject.GetPropertyValue("Name").ToString
            Next
            Value = vganame
        ElseIf InfoType = InfoTypes.VideocardMem Then
            For Each queryObject As ManagementObject In searcher.Get
                vgamem = queryObject.GetPropertyValue("AdapterRAM").ToString
            Next
            Value = Math.Round((((CDbl(Convert.ToDouble(Val(vgamem))) / 1024)) / 1024))
        End If
        Return Value



    End Function
    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        ToolStripComboBox1.Items.Clear()
        Sdriveer()
    End Sub
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Label15.Text = ""
        Timer2.Stop()
    End Sub
    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
        Dim count = ToolStripComboBox1.Items.Count
        Label9.Text = count & " Devices Are Connected"
    End Sub
    Private Sub Label9_Click(sender As Object, e As EventArgs) Handles Label9.Click

    End Sub

    Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
        If Label18.Text >= 1000 Then
            Information.Label17.Text = "Amount of RAM: " & Label18.Text & " GB"
        ElseIf Label18.Text <= 1000 Then
            Information.Label17.Text = "Amount of RAM: " & Label18.Text & " MB"
        End If
        If Label22.Text >= 1000 Then
            Information.Label21.Text = "Video Memory: " & Label22.Text & " GB"
        ElseIf Label22.Text <= 1000 Then
            Information.Label21.Text = "Video Memory: " & Label22.Text & " MB"
        End If
        Timer4.Stop()

    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Information.ShowDialog()
    End Sub
    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        Summary.ShowDialog()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Application.Exit()
        Application.ExitThread()

    End Sub
End Class

Recommended Answers

All 3 Replies

My untrained eye cannot see any hard coded reference to your external drive.

Is it possible that the application is finding something like a sd card reader and labeling it as a removable media?

FYI, there is also an Environment class. Exmaple : Click Here

When I run your Sdriveer sub routine it works properly and reports the drives on the computer it's running from. I suspect the computer your testing it with has an E drive and that's why you're seeing an entry for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.