Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

vb.net - How to check if the text file is open and close the text file?

I am trying to save the text file in this path:"C:Testest.txt" and when the file is already opened I need to check whether the file is opened and I need to close it before writing it to the file.

Here is the code for saving the file: Dim myfile As String = "C:Testest.txt"

    'Check if file exists
    If System.IO.File.Exists(myfile) = True Then
        'Delete it!

        Dim fi As New FileInfo(myfile)
        fi.Delete()
    End If



    Using sfdlg As New Windows.Forms.SaveFileDialog
        sfdlg.DefaultExt = "amk"
        sfdlg.Filter = "AquaMark Project|*.amk"
        If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then


            Dim SaveData As New gCanvasData


            IO.Directory.CreateDirectory("C:Test")
            Dim w As New IO.StreamWriter("C:Testest.txt")
            Dim i As Integer


            For i = 0 To CheckedListBox1.Items.Count - 1
                w.WriteLine(CheckedListBox1.Items.Item(i))
            Next
            w.Close()
            With SaveData
                frmDisplay.GCanvas1.UnselectCurrentAnotate()
                .gAnnotates = frmDisplay.GCanvas1.gAnnotates
                .Image = frmDisplay.GCanvas1.Image
            End With

            Using objStreamWriter As New StreamWriter(sfdlg.FileName)
                Dim x As New XmlSerializer(GetType(gCanvasData))
                x.Serialize(objStreamWriter, SaveData)
                objStreamWriter.Close()
            End Using
        End If
    End Using

If I am doing this way I am able to close the notepad process but I need to close the specific opened text file:

Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
Process() = CType(Interaction.GetObject("C:Testest.txt"), Diagnostics.Process())
For Each p As Process In Process
    p.Kill()
Next 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...