Reputation: 677
I am working on a button that will delete text in gross.txt. I keep getting this error:
The process cannot access the file 'C:\Users\isaiah\Visual BASIC Programs\VB2012\Chap10\Gross Pay Solution\Gross Pay Project\bin\Debug\gross.txt' because it is being used by another process.
Or more specifically:
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll.
It appears the error comes from btnDelete_Click at the bottom.
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub txtGrossPay_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtGrossPay.KeyPress
' Allows the text box to accept only numbers, the period, and the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim outFile As IO.StreamWriter
outFile = IO.File.AppendText("gross.txt")
outFile.WriteLine(txtGrossPay.Text)
outFile.Close()
txtGrossPay.Text = ""
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' Declare new inFile Varable as a streamreader object
Dim inFile As IO.StreamReader
Dim dblLine As Double
' Opens gross.txt for input
inFile = IO.File.OpenText("gross.txt")
' .Exists() searches /bin folder for gross.txt, returns a boolean value
If IO.File.Exists("gross.txt") Then
inFile = IO.File.OpenText("gross.txt")
'Fill the list with the values
Do Until inFile.Peek = -1
Double.TryParse(inFile.ReadLine, dblLine)
lstContents.Items.Add(dblLine.ToString("C2").PadLeft(6, " "c))
Loop
Else
MessageBox.Show("The file you have requested does not exist", "Gross Pay Project",
MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
End If
inFile.Close()
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim outDelete As IO.StreamWriter
outDelete = IO.File.CreateText("gross.txt")
outDelete.Close()
End Sub
End Class
Upvotes: 0
Views: 1341
Reputation: 373
The error means what it says. Somewhere you have the file open. Perhaps in Notepad? Are you looking at the contents of the file perhaps?
Upvotes: 0
Reputation: 188
Ok, I found the issue.
You were opening the file twice in your code with the line
inFile = IO.File.OpenText("gross.txt")
I commented out the first time you did it before your .Exists call. I also added a dispose to your Delete call. CreateText doesn't need to assign to anything so I commented out that code and added the line you needed. I tested and these changes fixed the issue. In fact the first change was the fix, the second one was just to clean things up.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'declare new inFile Varable as a streamreader object
Dim inFile As IO.StreamReader
Dim dblLine As Double
'opens gross.txt for input
' inFile = IO.File.OpenText("gross.txt")
'.Exists() searches /bin folder for gross.txt, returns a boolean value
If IO.File.Exists("gross.txt") Then
inFile = IO.File.OpenText("gross.txt")
'fill the list with the values
Do Until inFile.Peek = -1
Double.TryParse(inFile.ReadLine, dblLine)
lstContents.Items.Add(dblLine.ToString("C2").PadLeft(6, " "c))
Loop
inFile.Close()
Else
MessageBox.Show("The file you have requested does not exist", "Gross Pay Project",
MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
'Dim outDelete As IO.StreamWriter
'outDelete = IO.File.CreateText("gross.txt")
'outDelete.Close()
IO.File.CreateText("gross.txt").Dispose
End Sub
Upvotes: 1
Reputation: 188
I believe you need to call dispose ...
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim outDelete As IO.StreamWriter
outDelete = IO.File.CreateText("gross.txt")
outDelete.Close()
outDelete.Dispose()
End Sub
Also in your Do Loop are you meaning to add a line when the
Double.TryParse(inFile.ReadLine, dblLine)
fails to parse? It seems like you would want to skip it if the parse fails
Do Until inFile.Peek = -1
if Double.TryParse(inFile.ReadLine, dblLine) then
lstContents.Items.Add(dblLine.ToString("C2").PadLeft(6, " "c))
end if
Loop
but up to you.
Upvotes: 1