0

Im having this problem and it doesnt seem to go away. Theres a code ive done in vb.net, in which i to to stream files but i keep on getting that error

"IOException was unhandled" The process cannot access the file 'C:\Users\Lenovo\documents\visual studio 2010\Projects\File streaming\File Streaming\bin\Debug\banking.xls' because it is being used by another process.

Heres the code:

Imports System.IO

Imports System.Windows.Forms.Form
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        Dim FileStr As New FileStream("banking.xls", FileMode.Create, FileAccess.Write)

        Dim a As New StreamWriter(FileStr)

        'a.WriteLine("File should be displayed in the RTB............")
        'a.Close()

       **ERROR APPEARS HERE**     
        FileStr = New FileStream("banking.xls", FileMode.Open, FileAccess.Read)

        Dim i As New StreamReader(FileStr)

        i.BaseStream.Seek(0, SeekOrigin.Begin)

        If i.Peek() > -1 Then

            rtbDisplay.Text &= i.ReadLine()

        End If
        i.Close()

    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click

        rtbDisplay.Text = ""

    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click

        Me.Close()

    End Sub

    Private Function FileStream() As Object
        Throw New NotImplementedException
    End Function

End Class
Fleury26
  • 585
  • 4
  • 19

1 Answers1

2

FileStream and StreamWriter are IDisposable objects, and should be disposed when you are done with them. when IDisposable have local scope, it is good practice to create them with a Using block.

Sub Main()
    Using FileStr As New FileStream("banking.xls", FileMode.Create, FileAccess.Write)
        Using a As New StreamWriter(FileStr)
            a.WriteLine("File should be displayed in the RTB............")
        End Using
    End Using

    Using FileStr = New FileStream("banking.xls", FileMode.Open, FileAccess.Read)
        Dim i As New StreamReader(FileStr)
    End Using
End Sub

Still, I don't know why you are writing text to an xls file like this. If you are manipulating Excel files, you should look at Open Xml or Microsoft Office Interop

djv
  • 15,168
  • 7
  • 48
  • 72
  • 1
    Mostly I agree about Open Xml or Interop. Sometimes, though, you can do an "excel lite" effect by working with a tab-delimited text file that has an xls extension. Excel will open this and treat it as a native file. You just need to be careful, because once the real Excel saves new info to that file, you're dealing with the full Excel format. – Joel Coehoorn Nov 15 '17 at 15:50
  • @JoelCoehoorn interesting. I work a lot with tab delim txt and *Open with >> Excel* often. After a quick test, you are right that it works with xls extension, though there's a warning message when opening the file. It doesn't work with the newer xlsx extension however. – djv Nov 15 '17 at 15:56
  • Id really appreciate your help with what you just mentioned! i have no clue as to what you are saying. im still new in this field – Ervin Matlapeng Nov 16 '17 at 09:14