setting up filesystem permissions

2019-07-09 10:41发布

问题:

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);





        }
    }
}

this is my code when i execute this the pop up is showing that Actually, this code is to create a folder rs and set its permission to deny full control but on running the error come with the message Some or all identity references could not be translated. What is the error?

回答1:

You should change the following line:

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

to:

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

Also if you look at the following Everyone Group there is an answer a bit further down that suggests you should use SSID's instead of the names.



回答2:

Try the group "Everyone", not *.



标签: c# .net-4.0