关闭编写器后,文件仍由进程使用

时间:2016-11-04 13:50:29

标签: .net vb.net visual-studio

最近,我正在处理文件。 所以我创建了一个" sub"谁打开一个文件读取它并直接关闭它。在另一方面,我重新打开它并重写它。但是有我的问题,当我这样做时,visual studio告诉我该文件已被一个进程使用。

如果您找到了某些内容,请提供我的代码,请告诉我们:

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

3 个答案:

答案 0 :(得分:1)

您必须使用Using子句

Using mStreamReader As New StreamReader(Path, True)

    ' write your code here

End Using

在此Article

中详细了解Using

您的代码将是这样的:

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

答案 1 :(得分:0)

您还可以使用

读取所有行

System.IO.File.ReadAllLines

System.IO.File.WriteAllLines

然后你可以这样做:

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

不需要Streams。一切都取决于文件的范围。更大的文件 - &gt;需要更多的记忆。

如果问题仍然存在,该文件可能在其他地方打开。

答案 2 :(得分:0)

很抱歉长时间没有回答。 我回到这个项目,我找到了答案。在这个程序之前有一个打字机是开放的并且没有关闭。所以现在它运作良好。

最好的关心每个人