c# - 设置文件系统权限

标签 c# .net-4.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string Directoryd = "D:";
            string mydirectory = Directoryd + "\\" + "rs\\";
            if (!Directory.Exists(mydirectory))
            {
                Directory.CreateDirectory(mydirectory); 
            }
            DirectoryInfo di = new DirectoryInfo(mydirectory);
            DirectorySecurity ds = di.GetAccessControl();


            ds.AddAccessRule(new FileSystemAccessRule(@"*",FileSystemRights.FullControl,AccessControlType.Allow));
           di.SetAccessControl(ds);





        }
    }
}

这是我的代码,当我执行此操作时,弹出窗口显示 实际上,这段代码是创建一个文件夹 rs 并将其权限设置为拒绝完全控制,但在运行时出现错误并带有消息 部分或全部身份引用无法翻译。 错误是什么?

最佳答案

您应该更改以下行:

 ds.AddAccessRule(new FileSystemAccessRule(@"*",FileSystemRights.FullControl,AccessControlType.Allow));

至:

 ds.AddAccessRule(new FileSystemAccessRule(@"Everyone",FileSystemRights.FullControl,AccessControlType.Allow));

此外,如果您查看以下内容Everyone Group下面有一个答案建议您应该使用 SSID 而不是名称。

关于c# - 设置文件系统权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472974/

相关文章:

c# - 使用列表扩展如何检查值是否存在并且我没有超出范围?

c# - crdb_adoplus.dll单元测试解决方案

c# - AspNet Core 集成测试,将参数传递给 WebApplicationFactory

entity-framework - 可怕的 "parameter was not bound in the specified LINQ to Entities query expression"异常

.net - "reference was created to embedded interop assembly"是什么意思?

c# - 获取给定名称的属性的默认值

c# - 在 Raspberry PI 上管理 Wifi 连接

c# - 如何判断是鼠标选中还是按键选中?

c# - 不受约束的旋律错误

wcf - 我可以将连接字符串与 WCF 中的终结点相关联吗?