How to check if the text file is open and close the text file?

后端 未结 5 1093
忘了有多久
忘了有多久 2020-12-06 15:38

I am trying to save the text file in this path:\"C:\\Test\\test.txt\" and when the file is already opened I need to check whether the file is opened and I need to close it b

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 16:10

    I do not believe there is a property that will allow for you to check if the streamreader is open or not.

    Best practice seems to be to .close the reader when done with it. (All in the method that it was used in.)

    You could try a try block to handle the exception if you are still getting one.

    May be able to find additional info and some sample code here. Good Luck.

    MSDN! StreamReader

    EDIT: You may be able to check using this. IO.File

    Private Function CheckFile(ByVal filename As String) As Boolean
    
        Try
            System.IO.File.Open(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
            FileClose(1)
            Return False
        Catch ex As Exception
            Return True
        End Try
    
    End Function
    

提交回复
热议问题