Preventing a file from deletion and change

2019-09-04 13:58发布


I have a text file. I need to the following:
1. Prevent remove a file.
2. Prevent changes to the file.
Even if the program is not running. Is this possible? Maybe through the use of the service?
Users of application can add new data to a file.

General: I need to prevent my file from delete and any changes (outside of my app).
Thanks.

标签: c# file locking
2条回答
家丑人穷心不美
2楼-- · 2019-09-04 14:35

This would be something you do with file permissions. Inside of the files security settings, you can set who can delete the file, who can modify the file, etc. You could have the application elevate its rights to be able to modify the file.

If the application is not running, it cannot protect the file itself. Running the application as a service would allow it to stay running, which would allow it to perform some protective actions on the file but you would need a second application to interact with the user then and you would also be doing the job of the operating system. Elevating rights inside of the C# application is much easier than attempting to do what you are doing through code.

查看更多
迷人小祖宗
3楼-- · 2019-09-04 14:41

You can use Windows RBS to ensure that the file can only be modified or deleted by Administrators and a specific system user which your program creates and can be run in that context.

NEVER block Administrators' access to files; you will catch seven levels of hell from your application's users, who can't remove this one file (and thus can't remove the installation folder) when uninstalling your app.

查看更多
登录 后发表回答