Click here to Skip to main content
14,324,872 members
Rate this:
Please Sign up or sign in to 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:

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.
Richard MacCutchan 15-Nov-17 10:49am
   
Also, this code will not create or read Excel files.
Ervin Matlapeng 16-Nov-17 3:11am
   
Is there a way of doing that? please enlighten me
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?
Ervin Matlapeng 16-Nov-17 7:57am
   
The information in the excel spreadsheet should appear in vb.net
Richard MacCutchan 16-Nov-17 9:12am
   
OK, well the first thin g you need to learn is how to read (and write) Excel files. You can either use the Microsoft.Office.Interop.Excel namespace ()[^], or the Jet or ACE database driver as described in Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].
Ramza360 16-Nov-17 12:13pm
   
Good catch there as well ;)

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
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)
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100