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?
Thankyou in advance,
Ben
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
Ben
Last edited: