Results 1 to 3 of 3

Thread: [RESOLVED] How to solve IO error?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Resolved [RESOLVED] How to solve IO error?

    Hi,
    I'm trying to get drives total size (including Hard drives) and free space using drive.TotalSize property. But it gives IO Exception Error and says that drive is not ready.
    While drive.IsReady property tells hard drives are ready. What should I do?
    Name:  1.jpg
Views: 285
Size:  30.7 KB
    Name:  2.png
Views: 239
Size:  7.2 KB
    Code:
    Imports System.IO
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ListView1.Items.Clear()
            Dim i As Integer = 0
            For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
                Dim itemText As String = drive.Name
                Dim itemSize As Long= drive.TotalSize
                Dim Type As String
                Dim ltr As String = drive.Name
                If drive.IsReady AndAlso drive.VolumeLabel <> "" Then
                    itemText = drive.VolumeLabel
                Else
                    Select Case drive.DriveType
                        Case IO.DriveType.Fixed : itemText = "Local Disk"
                        Case IO.DriveType.CDRom : itemText = "CD-ROM"
                        Case IO.DriveType.Network : itemText = "Network Drive"
                        Case IO.DriveType.Removable : itemText = "Removable Disk"
                        Case IO.DriveType.Unknown : itemText = "Unknown"
    
                    End Select
    
                End If
                Select Case drive.DriveType
                    Case IO.DriveType.Fixed : Type = "Local Disk"
                    Case IO.DriveType.CDRom : Type = "CD-ROM"
                    Case IO.DriveType.Network : Type = "Network Drive"
                    Case IO.DriveType.Removable : Type = "Removable Disk"
                    Case IO.DriveType.Unknown : Type = "Unknown"
                End Select
                ListView1.Items.Add(itemText)
                ListView1.Items(i).SubItems.Add(ltr)
                ListView1.Items(i).SubItems.Add(Type)
                ListView1.Items(i).SubItems.Add(itemSize)
                i += 1
    
            Next
        End Sub
    
        Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
            Dim SelItem As String = ListView1.SelectedItems(0).SubItems(1).Text
            For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
                Try
                    Process.Start(SelItem)
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical, "ERROR")
    
                End Try
            Next
        End Sub
    
        Private Sub ListView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyDown
            If e.KeyCode = Keys.Enter Then
                Dim SelItem As String = ListView1.SelectedItems(0).SubItems(1).Text
                For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
                    Try
                        Process.Start(SelItem)
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical, "ERROR")
    
                    End Try
                Next
            End If
        End Sub
    
    
    End Class

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How to solve IO error?

    Instead of having this:
    Code:
                Dim itemSize As Long= drive.TotalSize
                ...
                ListView1.Items(i).SubItems.Add(itemSize)
    You could do something like this:
    Code:
                ...
                If drive.IsReady Then
                    Dim itemSize As Long= drive.TotalSize
                    ListView1.Items(i).SubItems.Add(itemSize)
                Else
                    ListView1.Items(i).SubItems.Add("(not ready)")
                End If

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Re: [RESOLVED] How to solve IO error?

    Thank you. The problem solved.

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