如何使用C#为文件系统上的文件添加用户读/写权限 [英] How to add user read/write permission to a file on the file system using C#

查看:113
本文介绍了如何使用C#为文件系统上的文件添加用户读/写权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我一直在谷歌这个话题一段时间,似乎找不到任何有用的东西。我正在编写一个应用程序,用户从模板中创建(单词)文档,文档在创建时保存,并打开供用户编辑。一旦用户完成编辑并保存文档(其他一些数据存储到sql数据库并发生其他一些操作),代码必须为此文档和同一部门中的其他一些用户设置读取权限(根据一些商业逻辑)。



所以我的问题是如何以C#代码以编程方式设置此文件的读取权限?



谢谢你找到了答案!

B

Hello, I've been googleing this topic for some time now and can't seem to find anything useful. I'm writing an application where the user crates a (word) document from a template, the document is saved upon creation and is opened for the user to edit it. Once the user is done editing and saves the document (some other data is stored to the sql database and some other actions happen) the code must set read permissions to the document for this and some other user that are in the same department (according to some business logic).

So my question is how to programaticly set read permissions for this file in C# code?

Thank you for your answer!
B

推荐答案

你好请使用这个以编程方式授予访问文件的权限。



Hi Please use this one to give programatically permission to access of file.

FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Read, "C:\\test_r");
f2.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, "C:\\example\\out.txt");





在这里你可以设置文件的路径而不是C:\ \ test_r



Here you can set path of your file instead of "C:\\test_r"


我使用不同的方法:将文件移动到没有用户访问权限的地方。如果需要访问,请将其复制到用户可以访问的位置,然后再将其删除。
I'd use a different approach: move the file to a place where no user has access to. And if access is required, copy it to a position where the user can access it, afterwards remove it again.


http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity.aspx [ ^ ]



这对我很有用......
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity.aspx[^]

This worked great for me...


这篇关于如何使用C#为文件系统上的文件添加用户读/写权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆