Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I busy trying file Streaming but i keep on getting the above mentioned error more especially accompanied by:

"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."

but the file is not opened or used by any other program

What I have tried:

VB
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()

        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
Posted
Updated 15-Nov-17 3:27am
v2
Comments
Ramza360 15-Nov-17 9:31am    
Yup you create it and never close it before trying to open it for reading.
Ervin Matlapeng 16-Nov-17 3:11am    
Is there a way of doing that? please enlighten me
Richard MacCutchan 15-Nov-17 10:49am    
Also, this code will not create or read Excel files.
Ervin Matlapeng 16-Nov-17 3:12am    
What should i do exactly?
Richard MacCutchan 16-Nov-17 3:54am    
I don't know, what exactly are you trying to achieve?

1 solution

Quote:
but the file is not opened or used by any other program
The file is not opened by another process but by your own application!

You have commented the Close call:
VB
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............")

' This must be uncommented!
'a.Close()

'Or dispose or close the FileStream instead
FileStr.Dispose()
'FileStr.Close()

'This fails when the file is still opened
FileStr = New FileStream("banking.xls", FileMode.Open, FileAccess.Read)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900