Results 1 to 11 of 11

Thread: [RESOLVED]IOException was unhandled

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    [RESOLVED]IOException was unhandled

    Basically I'm trying to save the source of a site and then read certain lines, this works once. The second time I get this error: "IOException was unhandled"
    Code:
        Public Function GetNumberOfLines(ByVal File_Path As String) As Integer
            Dim SR As New StreamReader(File_Path)
            Dim NumberOfLines As Integer
            Do While SR.Peek >= 0
                SR.ReadLine()
                NumberOfLines += 1
            Loop
            Return NumberOfLines
            SR.Close()
        End Function
    
        Public Function ReadALine(ByVal File_Path As String, ByVal TotalLines As Integer, ByVal Line2Read As Integer) As String
            Dim Buffer As Array
            Dim Line As String
            If TotalLines < Line2Read Or Line2Read = 0 Then
                Return "No such line exists"
            Else
                Line2Read -= 1
                Buffer = File.ReadAllLines(File_Path)
                Line = Buffer(Line2Read)
                Return Line
            End If
        End Function
    
        Private Function StealCar()
            CarTheft = ReadALine(log, GetNumberOfLines(log), 58)
            CarTheft1 = Microsoft.VisualBasic.Left(ReadALine(log, GetNumberOfLines(log), 58), 6)
            CarTheftC = ReadALine(log, GetNumberOfLines(log), 68)
            CarTheftC1 = Microsoft.VisualBasic.Left(ReadALine(log, GetNumberOfLines(log), 68), 6)
    
            If CarTheft1 = "oTimer" Then
                If Len(CarTheft) = "20" Then
                    CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 58), 5)), 3)
                ElseIf Len(CarTheft) = "19" Then
                    CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 58), 4)), 2)
                ElseIf Len(CarTheft) = "18" Then
                    CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 58), 3)), 1)
                End If
            ElseIf CarTheftC1 = "oTimer" Then
                If Len(CarTheftC) = "20" Then
                    CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 68), 5)), 3)
                ElseIf Len(CarTheftC) = "19" Then
                    CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 68), 4)), 2)
                ElseIf Len(CarTheftC) = "18" Then
                    CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 68), 3)), 1)
                End If
            Else
                CarTimer1 = "0"
    
    
            End If
    
        End Function
    
        Private Function DoCrimes()
            DoCrime = ReadALine(log, GetNumberOfLines(log), 55)
            DoCrime1 = Microsoft.VisualBasic.Left(ReadALine(log, GetNumberOfLines(log), 55), 6)
            If DoCrime1 = "oTimer" Then
                If Len(DoCrime) = "20" Then
                    CrimeTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 55), 5)), 3)
                ElseIf Len(DoCrime) = "19" Then
                    CrimeTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 55), 4)), 2)
                ElseIf Len(DoCrime) = "18" Then
                    CrimeTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 55), 3)), 1)
                End If
            Else
                CrimeTimer1 = "0"
    
            End If
        End Function
    
        Private Sub GetSource()
            Dim savef As New System.IO.StreamWriter(log)
            savef.Write(source)
            savef.Close()
            DoCrimes()
            StealCar()
        End Sub
    I've searched and I found quite a few of these threads, it's about not closing something correctly but I just can't figure out what I'm doing wrong
    I call GetSource() to make this work

    I've been having trouble with this for a while now, so yea any help is much appreciated
    Last edited by Xeno154; Jul 19th, 2009 at 03:26 PM. Reason: highlighted the problem

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: IOException was unhandled

    Use some error trapping, ie Try... Catch blocks to give us more information then the type of exception that was unhandled. Place them around where you are performing code on files.

    vb.net Code:
    1. Try
    2.     'Code here
    3. Catch ex as IOException
    4.     MessageBox.Show(ex.Message)
    5. Catch ex as Exception
    6.     MessageBox.Show(ex.Message)
    7. End Try

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Re: IOException was unhandled

    Okay I did that, this is what's causing the problem:
    Code:
      Dim savef As New System.IO.StreamWriter(log)

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: IOException was unhandled

    Ok, and what is the message? Is the file still in a open state?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Re: IOException was unhandled

    I don't really know how to find the message because:
    Code:
            Try
                Dim savef As New System.IO.StreamWriter(log)
            Catch ex As IOException
                MessageBox.Show(ex.Message)
    
                savef.Write(source)
    
                savef.Close()
                DoCrimes()
                StealCar()
            End Try
    If I try to do this it says savef isn't declared

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: IOException was unhandled

    It should look like this:
    vb.net Code:
    1. Try
    2.             Dim savef As New System.IO.StreamWriter(log)
    3.             savef.Write(source)
    4.             savef.Close()
    5.         Catch ex As IOException
    6.             MessageBox.Show(ex.Message)
    7.         End Try
    8.         DoCrimes()
    9.         StealCar()

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Re: IOException was unhandled

    It's in dutch but basically it's saying
    Can't get access to file
    C:\Users\Frederic\Documents\stuff\log.txt because it's being used by another proces.
    btw I'm still quite new to all this so please forgive me if I make stupid mistakes

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: IOException was unhandled

    Looks like there is more to your code then. Where do you call GetSource? Also, where is 'source' declared?

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Re: IOException was unhandled

    This is the complete code also thanks for trying to help me out
    vb.net Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.     Dim d As HtmlDocument
    5.  
    6.     Dim source, log, waitloaded, CarTheft, CarTheft1, CarTheftC, CarTheftC1, CarTimer1, DoCrime, DoCrime1, CrimeTimer1 As String
    7.     Private Function GetCurrentWebDoc() As mshtml.HTMLDocument
    8.         Try
    9.             Return DirectCast(wb.Document, mshtml.HTMLDocument)
    10.         Catch ex As Exception
    11.             Return Nothing
    12.         End Try
    13.     End Function
    14.     Private Function GetCurrentWebForm() As mshtml.HTMLFormElement
    15.         Try
    16.             If GetCurrentWebDoc.forms.length > 0 Then
    17.                 Return DirectCast(GetCurrentWebDoc.forms.item(0), mshtml.HTMLFormElement)
    18.             Else
    19.                 Return Nothing
    20.             End If
    21.         Catch ex As Exception
    22.             Return Nothing
    23.         End Try
    24.     End Function
    25.     Public Function GetNumberOfLines(ByVal File_Path As String) As Integer
    26.  
    27.         Dim SR As New StreamReader(File_Path)
    28.         Dim NumberOfLines As Integer
    29.         Do While SR.Peek >= 0
    30.             SR.ReadLine()
    31.             NumberOfLines += 1
    32.         Loop
    33.         Return NumberOfLines
    34.         SR.Close()
    35.  
    36.     End Function
    37.     Private Sub NavInformation()
    38.         If wb.Url.ToString() = "http://www.barafranca.com/" Then
    39.             wb.Document.DomDocument.All("email").Value = "username"
    40.             wb.Document.DomDocument.All("pass").Value = "password"
    41.  
    42.             For y = 0 To wb.Document.DomDocument.Forms.length - 1
    43.                 For x = 0 To wb.Document.DomDocument.Forms(y).elements.length - 1
    44.                     If wb.Document.DomDocument.Forms(y).elements(x).Value = "Login" Then
    45.                         wb.Document.DomDocument.Forms(y).elements(x).Click()
    46.                         Exit For
    47.                     End If
    48.                 Next x
    49.             Next y
    50.         End If
    51.         If wb.Url.ToString() = "http://www.barafranca.com/game.php" Then
    52.             wb.Navigate("http://www.barafranca.com/information.php")
    53.         End If
    54.     End Sub
    55.     Private Function StealCar()
    56.         CarTheft = ReadALine(log, GetNumberOfLines(log), 58)
    57.         CarTheft1 = Microsoft.VisualBasic.Left(ReadALine(log, GetNumberOfLines(log), 58), 6)
    58.         CarTheftC = ReadALine(log, GetNumberOfLines(log), 68)
    59.         CarTheftC1 = Microsoft.VisualBasic.Left(ReadALine(log, GetNumberOfLines(log), 68), 6)
    60.  
    61.         If CarTheft1 = "oTimer" Then
    62.             If Len(CarTheft) = "20" Then
    63.                 CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 58), 5)), 3)
    64.             ElseIf Len(CarTheft) = "19" Then
    65.                 CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 58), 4)), 2)
    66.             ElseIf Len(CarTheft) = "18" Then
    67.                 CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 58), 3)), 1)
    68.             End If
    69.         ElseIf CarTheftC1 = "oTimer" Then
    70.             If Len(CarTheftC) = "20" Then
    71.                 CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 68), 5)), 3)
    72.             ElseIf Len(CarTheftC) = "19" Then
    73.                 CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 68), 4)), 2)
    74.             ElseIf Len(CarTheftC) = "18" Then
    75.                 CarTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 68), 3)), 1)
    76.             End If
    77.         Else
    78.             CarTimer1 = "0"
    79.  
    80.  
    81.         End If
    82.  
    83.     End Function
    84.     Public Function ReadALine(ByVal File_Path As String, ByVal TotalLines As Integer, ByVal Line2Read As Integer) As String
    85.  
    86.         Dim Buffer As Array
    87.         Dim Line As String
    88.         If TotalLines < Line2Read Or Line2Read = 0 Then
    89.             Return "No such line exists"
    90.         Else
    91.             Line2Read -= 1
    92.             Buffer = File.ReadAllLines(File_Path)
    93.             Line = Buffer(Line2Read)
    94.             Return Line
    95.         End If
    96.  
    97.     End Function
    98.     Private Function DoCrimes()
    99.         DoCrime = ReadALine(log, GetNumberOfLines(log), 55)
    100.         DoCrime1 = Microsoft.VisualBasic.Left(ReadALine(log, GetNumberOfLines(log), 55), 6)
    101.  
    102.         If DoCrime1 = "oTimer" Then
    103.             If Len(DoCrime) = "20" Then
    104.                 CrimeTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 55), 5)), 3)
    105.             ElseIf Len(DoCrime) = "19" Then
    106.                 CrimeTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 55), 4)), 2)
    107.             ElseIf Len(DoCrime) = "18" Then
    108.                 CrimeTimer1 = Microsoft.VisualBasic.Left((Microsoft.VisualBasic.Right(ReadALine(log, GetNumberOfLines(log), 55), 3)), 1)
    109.             End If
    110.         Else
    111.             CrimeTimer1 = "0"
    112.  
    113.         End If
    114.  
    115.  
    116.     End Function
    117.     Private Sub GetTimerSource()
    118.         If wb.Url.ToString = "http://www.barafranca.com/information.php" Then
    119.             Timer2.Enabled = "True"
    120.  
    121.         End If
    122.     End Sub
    123.     Private Sub GetSource()
    124.  
    125.         Try
    126.  
    127.             Dim savef As New System.IO.StreamWriter(log)
    128.  
    129.             savef.Write(source)
    130.  
    131.             savef.Close()
    132.  
    133.         Catch ex As IOException
    134.  
    135.             MessageBox.Show(ex.Message)
    136.  
    137.         End Try
    138.         DoCrimes()
    139.         'StealCar()
    140.     End Sub
    141.  
    142.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    143.         log = "C:\Users\Frederic\Documents\stuff\log.txt"
    144.     End Sub
    145.  
    146.     Private Sub wb_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted
    147.         Dim d As HtmlDocument
    148.         d = wb.Document
    149.         source = d.Body.InnerHtml
    150.         NavInformation()
    151.         UrlText.Text = wb.Url.ToString()
    152.  
    153.  
    154.     End Sub
    155.  
    156.  
    157.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click
    158.         wb.Navigate(UrlText.Text)
    159.     End Sub
    160.  
    161.     Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    162.         CrimeTimer1 = CrimeTimer1 - 1
    163.         CarTimer1 = CarTimer1 - 1
    164.         If CarTimer1 < 0 Then
    165.             CarTimer1 = 0
    166.         End If
    167.         If CrimeTimer1 < 0 Then
    168.             CrimeTimer1 = 0
    169.         End If
    170.         CrimeTime.Text = CrimeTimer1
    171.         CarTime.Text = CarTimer1
    172.     End Sub
    173.  
    174.     Private Sub TestButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TestButton.Click
    175.         GetSource()
    176.     End Sub
    177.  
    178.     Private Sub wb_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles wb.Navigated
    179.  
    180.         GetTimerSource()
    181.     End Sub
    182.  
    183.     Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    184.         waitloaded = waitloaded + 1
    185.         If waitloaded > 1 Then
    186.  
    187.             waitloaded = 0
    188.             Timer2.Enabled = "false"
    189.         End If
    190.     End Sub
    191. End Class

  10. #10
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: IOException was unhandled

    First thing I recommend is turning Option Strict on... you have a lot of implicit conversions. Next try replacing these 3 subs:

    vb.net Code:
    1. Private Sub GetSource()
    2.  
    3.         Try
    4.             Using savef As New IO.StreamWriter(log)
    5.                 savef.Write(source)
    6.             End Using
    7.         Catch ex As Exception
    8.             MessageBox.Show(ex.Message)
    9.         End Try
    10.         DoCrimes()
    11.         'StealCar()
    12.     End Sub
    13.  
    14.     Public Function ReadALine(ByVal File_Path As String, ByVal TotalLines As Integer, ByVal Line2Read As Integer) As String
    15.         Try
    16.             Dim filecontents() As String = IO.File.ReadAllLines(File_Path)
    17.             If filecontents.Length - 1 >= Line2Read Then
    18.                 Return (filecontents(Line2Read))
    19.             Else
    20.                 Return String.Empty
    21.             End If
    22.         Catch ex As Exception
    23.             Throw ex
    24.         End Try
    25.     End Function
    26.  
    27.     Public Function GetNumberOfLines(ByVal File_Path As String) As Integer
    28.         Try
    29.             Return IO.File.ReadAllLines(File_Path).Length
    30.         Catch ex As Exception
    31.             Throw ex
    32.         End Try
    33.     End Function

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    9

    Re: IOException was unhandled

    That worked!
    Thanks so much for helping me, without you I'd still be stuck with this problem, I really had no idea how to solve this.
    again, thank you!

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