简体   繁体   中英

How to prevent a user from changing a file manually?

In a WPF application I use .txt files for holding some information. An application can read and write data from/to .txt file. Everything is OK, but the problem is that, to achieve this purpose, I have to grant writing access rights to these files for a user of an application and so, he/she gets the possibility to edit these files manually.

How could I set editing .txt files access rights for an application without granting the same level of rights to a user?

Edited (added):

After getting some comments and answers, I put the question this way (just to make my question more clear and not restricted to user access rights scope): How I could prevent the user from changing the file manually?

加密或数字签名

I'm going to presume that you are not trying to prevent the user from changing the file manually, you just want to prevent the extra step of specifically assigning rights to the file.

You are most likely writing to a file that is in a protected area (an area that became protected after UAC was introduced). To avoid this, write your file to one of the "approved" areas, such as %APPDATA%. Here is a list of a few more (assuming C is your boot drive):

C:\Users\username\Documents
C:\Users\username\AppData\Local
C:\Users\username\AppData\Roaming
C:\Users\Public\Documents
C:\ProgramData

This article has a whole bunch of info around that which you can cherry pick bits from.

This might be an overkill, but you could create a service that runs on a different user account, which can edit the file. Then your application would use that service to access the file.

This way you can prevent unwanted changes and/or log every change to the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM