Question Prog appends a textfile. Don't want user able to delete or modify while form open

Birthday

New member
Joined
May 17, 2012
Messages
2
Programming Experience
5-10
Prog appends a textfile. Don't want user able to delete or modify while form open. How do I do this?? - As long as form is open user cannot modify or delete textfile. But so the file can be edited from within form

How do I do this.
Here is my code so far :)
Public Class Form1
Dim FILE_NAME As String = "C:\Users\Birthday\Documents\info.txt"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
For i = 0 To 4
objWriter.WriteLine(InputBox("Enter info"))
Next
objWriter.Close()
End Sub
End Class
 
You should find that between opening the file, i.e. creating the StreamReader, and closing it, the file cannot be modified or deleted. That means that if you open the file when you open the form and close the file when you close the form, you'll get the behaviour you want.

If you find that that's not the case then you can change the way you open the file slightly to make sure it is. You can open the file by creating a FileStream and the constructor allows you to specify things like exclusive access. You can then pass that FileStream to the StreamWriter constructor instead of the file name.
 
Back
Top