There is a configuration file in a small program written recently that is critical to the entire program, and if it is deleted or rewritten, the entire program cannot run, or it cannot be closed after it has been run. So I've been looking for a way to manually overwrite a file.
For "Delete" is very easy to solve, the program to find the path of the configuration file, if empty then create, and give some default values. VB sample code is as follows:
FileName = App.Path + "\config"
' If the file does not exist, create the file if
Dir (FileName) = "" Then
open FileName for Output as #1 ' open order File, we can use Open statement
a = Encode ("123") + VbCrLf + "Ten" + vbCrLf ' vbCrLf for carriage return
Print #1, a ' write data
close #1 ' closes file
end If
I have been powerless to rewrite the configuration file manually, and I tried to hide it in the program. VB sample code is as follows:
SetAttr filename, vbsystem Or vbhidden ' hidden file
But after all, the symptom is not the root cause, the document will still be rewritten. Then I thought about modifying the configuration file suffix method so that it was not easy to open the file manually, but there was always a way to open it. Finally, a simple solution is to open the configuration file in the program, and then manually not open it. VB sample code is as follows:
Open FileName for Binary as #99
Just remember that when the program rewrites the file, you have to close the open file, otherwise the rewrite will fail. VB sample code is as follows:
To sum up, the simple way to prevent a file from being overwritten is to open the file first in the program.
The above mentioned is the entire content of this article, I hope you can enjoy.