简体   繁体   English

如何防止用户手动更改文件?

[英]How to prevent a user from changing a file manually?

In a WPF application I use .txt files for holding some information. 在WPF应用程序中,我使用.txt文件来保存一些信息。 An application can read and write data from/to .txt file. 应用程序可以从/ .txt文件读取和写入数据。 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? 如何在不向用户授予相同级别权限的情况下为应用程序设置编辑.txt文件访问权限?

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). 您最有可能写入受保护区域(在引入UAC后受到保护的区域)中的文件。 To avoid this, write your file to one of the "approved" areas, such as %APPDATA%. 要避免这种情况,请将文件写入“已批准”区域之一,例如%APPDATA%。 Here is a list of a few more (assuming C is your boot drive): 这是一个更多的列表(假设C是您的启动驱动器):

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. 这样,您可以防止不必要的更改和/或记录对文件的每个更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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