How to determine if a text file is open?

Lance Turbo

New member
Joined
Apr 19, 2013
Messages
4
Programming Experience
Beginner
Hi,

I would like to determine if a text file is open or not before I overwrite it. The code below overwrites the text file regardless if it is open or not and I can't get the exception thrown. Has anyone got any advice how I can confirm if the text file is in use?

Dim saveFileDialog1 As New SaveFileDialog()
        Dim saveDialogResult As New DialogResult

        
        saveFileDialog1.Title = "Create new project..."
        saveFileDialog1.FileName = "MyProject"
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
        saveFileDialog1.CheckFileExists = False
        saveFileDialog1.CreatePrompt = False

        If saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'If the file is open an error should occur
Try
                Dim fOpen As IO.FileStream = IO.File.Open(saveFileDialog1.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
                fOpen.Close()
                fOpen.Dispose()
                fOpen = Nothing
            Catch e As IO.IOException
                MsgBox("File is already in use")
            End Try

            Dim myFileWriter = New StreamWriter(saveFileDialog1.FileName)

            If (myFileWriter IsNot Nothing) Then
                myFileWriter.WriteLine("line 1")
                myFileWriter.Close()
            End If
        End If 

Thankyou in advance,

Ben
 
Last edited:
Are you under the impression that writing to the file using the StreamWriter should fail because you previously opened the FileStream? You are closing the FileStream immediately after opening it so the file is closed again before you create the StreamWriter. If you try to create the StreamWriter before closing the FileStream then it will fail.

Or are you expecting the opening of the FileStream to fail because you already have the file open elsewhere?
 
The only way to see if a file is currently writable, is to open a FileStream to it with FileAccess.Write. That is what StreamWriter(path) does. If write access is not possible an exception is thrown.

FileShare flag controls subsequent access to the file. When you set it, it controls what FileAccess others can get while you have the file open. If others have opened the file before you, they have set a FileShare flag that may limit what FileAccess you can get.
The code below overwrites the text file regardless if it is open or not and I can't get the exception thrown.
Since you know the file is "open", that means the program that opened it allowed subsequent writing by setting this FileShare flag, or it is also possible the program read the file contents into its memory and closed the file, so the file is not actually open.
 
Thanks for your replies.

JMC,

Yes I read elsewhere that by trying to open the filestream will result in an error if the file is already open. However when I open the text file in notepad, then run the code above, open text file does not change, but when I close the file and re open it, the first line text will be changed to "Line 1". No error is generated leaving me wondering how I determine if a file is already open or not.

John H,

I'll have a play around and see if I can understand what is going on. This is all quite new to me.

Thanks,
Ben
 
Back
Top