Protect a file from being deleted

How can i create a file and make it where it cannot be deleted
Like windows does with a message
"access denied" ?
file is created like this:
Public Sub SaveTextFile(sFileName As String, txt As String)
        Dim ff As Integer
        ff = FreeFile
       Open sFileName For Output As #ff
            Print #ff, txt
         Close #ff
End Sub

Open in new window

Who is Participating?
Martin LissConnect With a Mentor Older than dirtCommented:
I don't really know but see this EE solution. If I were you I would create a small new project just to try it out and if you need more help I guess you'll need to ask a new question because I use XP.
Martin LissOlder than dirtCommented:
You can make a file read-only via code but that won't prevent someone from going to Widows Explorer and changing that. IMO your best bet is to hide it, but even then it can be found and deleted.
Martin LissOlder than dirtCommented:
What happens if the file is deleted?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

isnoend2001Author Commented:
The file is encrypted and reads:
"Not Registered" or
"refunded" or
"yes Registered"
Martin LissOlder than dirtCommented:
I would put the encrypted value in the Registry where it's less prone to erasure rather than a file and in either case if not found I'd assume that the app is "Not Registered".
isnoend2001Author Commented:
Thanks Martin, I thought writing to the registry was a problem when Vista came out,No ?
isnoend2001Author Commented:
Thanks Martin
I Also use xp, but have another computer with win7 just for testing.
Using the registry is too complicated and error prone
i have though o a few work arounds  that turn into dead ends
1. open the file when the app closes, but don't close the file. This may keep a reference to the file, not sure, but the next time the computer starts it will no longer work.
2. determine if there is any way to write a value to the resource file
Martin LissOlder than dirtCommented:
OK if you have other questions concerning your 1 and 2, ask a new question and I or others will try to help
QlemoBatchelor and DeveloperCommented:
"Using the registry is too complicated and error prone" - are you serious? It is less error prone than using a file, and not at all complicated once you have set it up correctly. Just three or four lines of code required.

On the other hand, accepting http:#a40446489 seems to be off, if you can't use the suggestion.
isnoend2001Author Commented:
I agree the registry was easy until vista
the  http:#a40446489 answer provided a link for info that reinforced what i thought
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.