0

我有一个作为本地系统运行的服务。我正在从服务创建一些文件夹。创建这些文件夹时不会授予 Windows 7 中“用户”组中的人员写入权限。

如何创建这些文件夹以授予每个人写入权限?

4

1 回答 1

0

您可以使用我在此示例中找到的逻辑

public class Permissions
{
    public void addPermissions(string dirName, string username)
    {
        changePermissions(dirName, username, AccessControlType.Allow);
    }

    public void revokePermissions(string dirName, string username)
    {
        changePermissions(dirName, username, AccessControlType.Deny);
    }

    private void changePermissions(string dirName, string username, AccessControlType newPermission)
    {
        DirectoryInfo myDirectoryInfo = new DirectoryInfo(dirName);

        DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();

        string user = System.Environment.UserDomainName + "\\" + username;

        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(
            user, 
            FileSystemRights.Read | FileSystemRights.Write | FileSystemRights.ExecuteFile | FileSystemRights.Delete, 
            InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
            PropagationFlags.InheritOnly, 
            newPermission
        ));

        myDirectoryInfo.SetAccessControl(myDirectorySecurity);
    }
}

我还发现这篇文章有其他例子

http://www.c-sharpcorner.com/uploadfile/babu_2082/adding-groups-user-names-and-permissions-for-a-directory-in-C-Sharp/

于 2013-08-16T15:21:21.930 回答