VB simple implementation to prevent files from being rewritten _vb

Source: Internet
Author: User

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:

Close #99 ' closes file

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.