将文件设置为只读(C#)

huangapple go评论158阅读模式
英文:

C# make file readonly

问题

我创建了一个Outlook插件,它会在邮件发送之前检查是否包含一些禁止的词汇。它会将包含邮件内容的字符串与一个.CSV文件中的词汇进行比较。

我不介意其他人能够阅读那个.CSV文件,但我不希望他们能够修改它,除非他们是在使用Outlook插件时进行修改。

为了实现这一点,我想要根据用户更改文件的属性。当使用插件时,管理员用户将获得读/写权限,而其他用户只能读取。然而,在插件之外,我希望该文件对所有人都是只读的。

我尝试使用File.SetAttributes("myPath", FileAttributes.ReadOnly);,但用户可以简单地在文件属性中取消选中"只读"选项,然后可以随意操作...我能否避免这种情况?是否有比只读模式更智能的选项?

英文:

I created an Outlook add-in that checks if some forbiden words are present in an email before it is sent. It simply compares a string containing the email with words present in a .CSV file.

I don't mind people being able to read that .CSV file, but I do not want them to be able to modify it, except if they are doing it using the Outlook add-in.

To do this, I wanted to change the properties of the file depending on the user. When using the addin, the admin user would get the Read/Write rights, whereas the otherones would only be able to read it. However, outside of the add-in, I want that file to be readonly for everyone.

I tried using File.SetAttributes("myPath", FileAttributes.ReadOnly); but then the user can simply go in the file properties, uncheck the "Readonly" option, and he can do whatever he wants ... Can I avoid this ? Is there a smarter option than the Readonly mod ?

答案1

得分: 1

你可以加密存储的文件。应用程序可以在内存中解密以执行任何操作。用户除非使用应用程序,否则无法修改。强制用户修改将损坏文件并使其无法使用。

英文:

You can encrypt the stored file.
To perform any operations, application can decrypt it in memory. User will not be able to modify unless using the application. Forced user modification will corrupt the file and make it unusable.

huangapple
  • 本文由 发表于 2020年1月6日 16:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定