|
-
Jul 19th, 2009, 01:22 PM
#1
Thread Starter
New Member
[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
-
Jul 19th, 2009, 01:42 PM
#2
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:
Try 'Code here Catch ex as IOException MessageBox.Show(ex.Message) Catch ex as Exception MessageBox.Show(ex.Message) End Try
-
Jul 19th, 2009, 02:46 PM
#3
Thread Starter
New Member
Re: IOException was unhandled
Okay I did that, this is what's causing the problem:
Code:
Dim savef As New System.IO.StreamWriter(log)
-
Jul 19th, 2009, 02:47 PM
#4
Re: IOException was unhandled
Ok, and what is the message? Is the file still in a open state?
-
Jul 19th, 2009, 02:50 PM
#5
Thread Starter
New Member
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
-
Jul 19th, 2009, 02:52 PM
#6
Re: IOException was unhandled
It should look like this:
vb.net Code:
Try Dim savef As New System.IO.StreamWriter(log) savef.Write(source) savef.Close() Catch ex As IOException MessageBox.Show(ex.Message) End Try DoCrimes() StealCar()
-
Jul 19th, 2009, 02:55 PM
#7
Thread Starter
New Member
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
-
Jul 19th, 2009, 02:59 PM
#8
Re: IOException was unhandled
Looks like there is more to your code then. Where do you call GetSource? Also, where is 'source' declared?
-
Jul 19th, 2009, 03:05 PM
#9
Thread Starter
New Member
Re: IOException was unhandled
This is the complete code also thanks for trying to help me out
vb.net Code:
Imports System.IO Public Class Form1 Dim d As HtmlDocument Dim source, log, waitloaded, CarTheft, CarTheft1, CarTheftC, CarTheftC1, CarTimer1, DoCrime, DoCrime1, CrimeTimer1 As String Private Function GetCurrentWebDoc() As mshtml.HTMLDocument Try Return DirectCast(wb.Document, mshtml.HTMLDocument) Catch ex As Exception Return Nothing End Try End Function Private Function GetCurrentWebForm() As mshtml.HTMLFormElement Try If GetCurrentWebDoc.forms.length > 0 Then Return DirectCast(GetCurrentWebDoc.forms.item(0), mshtml.HTMLFormElement) Else Return Nothing End If Catch ex As Exception Return Nothing End Try End Function 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 Private Sub NavInformation() If wb.Url.ToString() = "http://www.barafranca.com/" Then wb.Document.DomDocument.All("email").Value = "username" wb.Document.DomDocument.All("pass").Value = "password" For y = 0 To wb.Document.DomDocument.Forms.length - 1 For x = 0 To wb.Document.DomDocument.Forms(y).elements.length - 1 If wb.Document.DomDocument.Forms(y).elements(x).Value = "Login" Then wb.Document.DomDocument.Forms(y).elements(x).Click() Exit For End If Next x Next y End If If wb.Url.ToString() = "http://www.barafranca.com/game.php" Then wb.Navigate("http://www.barafranca.com/information.php") End If End Sub 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 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 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 GetTimerSource() If wb.Url.ToString = "http://www.barafranca.com/information.php" Then Timer2.Enabled = "True" End If End Sub Private Sub GetSource() Try Dim savef As New System.IO.StreamWriter(log) savef.Write(source) savef.Close() Catch ex As IOException MessageBox.Show(ex.Message) End Try DoCrimes() 'StealCar() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load log = "C:\Users\Frederic\Documents\stuff\log.txt" End Sub Private Sub wb_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted Dim d As HtmlDocument d = wb.Document source = d.Body.InnerHtml NavInformation() UrlText.Text = wb.Url.ToString() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click wb.Navigate(UrlText.Text) End Sub Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick CrimeTimer1 = CrimeTimer1 - 1 CarTimer1 = CarTimer1 - 1 If CarTimer1 < 0 Then CarTimer1 = 0 End If If CrimeTimer1 < 0 Then CrimeTimer1 = 0 End If CrimeTime.Text = CrimeTimer1 CarTime.Text = CarTimer1 End Sub Private Sub TestButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TestButton.Click GetSource() End Sub Private Sub wb_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles wb.Navigated GetTimerSource() End Sub Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick waitloaded = waitloaded + 1 If waitloaded > 1 Then waitloaded = 0 Timer2.Enabled = "false" End If End Sub End Class
-
Jul 19th, 2009, 03:17 PM
#10
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:
Private Sub GetSource() Try Using savef As New IO.StreamWriter(log) savef.Write(source) End Using Catch ex As Exception MessageBox.Show(ex.Message) End Try DoCrimes() 'StealCar() End Sub Public Function ReadALine(ByVal File_Path As String, ByVal TotalLines As Integer, ByVal Line2Read As Integer) As String Try Dim filecontents() As String = IO.File.ReadAllLines(File_Path) If filecontents.Length - 1 >= Line2Read Then Return (filecontents(Line2Read)) Else Return String.Empty End If Catch ex As Exception Throw ex End Try End Function Public Function GetNumberOfLines(ByVal File_Path As String) As Integer Try Return IO.File.ReadAllLines(File_Path).Length Catch ex As Exception Throw ex End Try End Function
-
Jul 19th, 2009, 03:26 PM
#11
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|