Vivien Goutey
Vivien Goutey

Reputation: 9

File still used by a processus after closing a streamwriter

Recently i'm on file manipulation. So i created a "sub" who open a file read it and close it at first hand. And in a second hand i reopen it and rewrite it. But there is my problem, when i do it visual studio tell me that the file is already use by a processus.

there is my code if you find something, let me know:

Function OpenTxt(ByVal Path As String) As String()

    'Variables
    Dim mStreamReader As StreamReader

    Dim Line As String
    Dim J As Integer
    Dim K As Integer
    Dim mStock() As String
    Dim TempTbl() As String
    Dim mSplit() As String


    'Ini
    If Not File.Exists(Path) Then

    End If
    mStreamReader = New StreamReader(Path, True)
    J = -1
    K = -1

    'Début
    Do
        Line = mStreamReader.ReadLine
        If Line <> "" Then
            mSplit = Split(Line, "||",, vbTextCompare)
            K = K + 1
            ReDim Preserve TempTbl(K)
            TempTbl(K) = Line

            Select Case mSplit(0)
                Case "IsOpen"
                    If mSplit(1) <> mEApp.LicenseKey Then

                    ElseIf mSplit(1) = 0 Then
                        TempTbl(K) = mSplit(0) & "||" & mEApp.LicenseKey
                    End If
                Case "STOCK"
                    J = J + 1
                    ReDim Preserve mStock(J)
                    mStock(J) = mSplit(1)
            End Select

        End If
    Loop Until Line Is Nothing

    mStreamReader.Close()
    mStreamReader = Nothing

    Dim mStreamWriter As StreamWriter
    mStreamWriter = New StreamWriter(Path, True)

    For K = 0 To UBound(TempTbl)
        mStreamWriter.WriteLine(TempTbl(K))
    Next

    mStreamWriter.Close()
    mStreamWriter = Nothing

    OpenTxt = mStock

    Return OpenTxt

End Function

Upvotes: 0

Views: 364

Answers (3)

Vivien Goutey
Vivien Goutey

Reputation: 9

Sorry for the long absence of answering. I'm back on this project and I find my answer. There was a streamwriter before this procedure who was open and wasn't closed. So for now it works well.

Best Regard to everyone

Upvotes: 0

Yahfoufi
Yahfoufi

Reputation: 2544

You have to use the Using clause

Using mStreamReader As New StreamReader(Path, True)

    ' write your code here

End Using

Learn more about Using in this Article

Your code will be like this:

Function OpenTxt(ByVal Path As String) As String()



    Dim Line As String
    Dim J As Integer
    Dim K As Integer
    Dim mStock() As String
    Dim TempTbl() As String
    Dim mSplit() As String

    'Ini
    If Not File.Exists(Path) Then

    End If
    Using mStreamReader As New StreamReader(Path, True)
        J = -1
        K = -1

        'Début
        Do
            Line = mStreamReader.ReadLine
            If Line <> "" Then
                mSplit = Split(Line, "||", , vbTextCompare)
                K = K + 1
                ReDim Preserve TempTbl(K)
                TempTbl(K) = Line

                Select Case mSplit(0)
                    Case "IsOpen"
                        If mSplit(1) <> mEApp.LicenseKey Then

                        ElseIf mSplit(1) = 0 Then
                            TempTbl(K) = mSplit(0) & "||" & mEApp.LicenseKey
                        End If
                    Case "STOCK"
                        J = J + 1
                        ReDim Preserve mStock(J)
                        mStock(J) = mSplit(1)
                End Select

            End If
        Loop Until Line Is Nothing

        mStreamReader.Close()

    End Using

    Using mStreamWriter As New StreamWriter(Path, True)

        For K = 0 To UBound(TempTbl)
            mStreamWriter.WriteLine(TempTbl(K))
        Next

        mStreamWriter.Close()

    End Using

    OpenTxt = mStock

    Return OpenTxt

End Function

Upvotes: 1

Gerren
Gerren

Reputation: 56

You could also read all lines using

System.IO.File.ReadAllLines

System.IO.File.WriteAllLines

Then you can just do it as follows:

Function OpenTxt(ByVal Path As String) As String()

    'Variables
    Dim Line As String
    Dim J As Integer
    Dim K As Integer
    Dim mStock() As String
    Dim TempTbl() As String
    Dim mSplit() As String

    Dim Lines as String() = IO.File.ReadAllLines(Path)
    J = -1
    K = -1

    'Début
    For Each Line in Lines
        If Line <> "" Then
            mSplit = Split(Line, "||",, vbTextCompare)
            K = K + 1
            ReDim Preserve TempTbl(K)
            TempTbl(K) = Line

            Select Case mSplit(0)
                Case "IsOpen"
                    If mSplit(1) <> mEApp.LicenseKey Then

                    ElseIf mSplit(1) = 0 Then
                        TempTbl(K) = mSplit(0) & "||" & mEApp.LicenseKey
                    End If
                Case "STOCK"
                    J = J + 1
                    ReDim Preserve mStock(J)
                    mStock(J) = mSplit(1)
            End Select

        End If
    Next

    IO.File.WriteAllLines(Path,TempTbl)

    OpenTxt = mStock

    Return OpenTxt

End Function

No need for Streams. All depends on the scope of the files. Bigger files -> more memory needed.

If problem stays, the file is probably open somewhere else.

Upvotes: 0

Related Questions