C#中目录的组权限

时间:2016-01-08 05:35:42

标签: c# .net file-permissions

我正在使用控制台应用程序visual studio 2012获取目录组的权限,但是收到错误"部分或全部身份引用无法翻译。"

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDirectory(@"C:\TestDirectory", @"NAVEEN\IIS_IUSRS");
        }
        public static void CreateDirectory(string DirectoryName, string UserAccount)
        {
            if (!System.IO.Directory.Exists(DirectoryName))Directory.CreateDirectory(DirectoryName);          
            AddUsersAndPermissions(DirectoryName, UserAccount, FileSystemRights.FullControl, AccessControlType.Allow);
        }
        public static void AddUsersAndPermissions(string DirectoryName, string UserAccount, FileSystemRights UserRights, AccessControlType AccessType)
        {
            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(DirectoryName);
                DirectorySecurity dirSecurity = directoryInfo.GetAccessControl();
                dirSecurity.AddAccessRule(new enter code hereFileSystemAccessRule(UserAccount, UserRights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessType));
                directoryInfo.SetAccessControl(dirSecurity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

我已经解决了我的问题。

我当地没有Active Directory Group。 DirectorySecurity,AddAccessRule 仅适用于 Active Directory组,因此 在客户端服务器上,我使用以下关键字从此sql端执行控制台应用程序:" xp_cmdshell" ,然后在 Active Directory组中创建了一个目录,并且时间没有得到蚂蚁类型的错误。

答案 1 :(得分:0)

从组中删除PC的名称,您只需要" IIS_USRS"。

通过这一次更改,以下代码可以毫无问题地运行。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDirectory(@"C:\TestDirectory", @"IIS_IUSRS");
        }
        public static void CreateDirectory(string DirectoryName, string UserAccount)
        {
            if (!System.IO.Directory.Exists(DirectoryName)) Directory.CreateDirectory(DirectoryName);
            AddUsersAndPermissions(DirectoryName, UserAccount, FileSystemRights.FullControl, AccessControlType.Allow);
        }
        public static void AddUsersAndPermissions(string DirectoryName, string UserAccount, FileSystemRights UserRights, AccessControlType AccessType)
        {
            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(DirectoryName);
                DirectorySecurity dirSecurity = directoryInfo.GetAccessControl();
                dirSecurity.AddAccessRule(new FileSystemAccessRule(UserAccount, UserRights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessType));
                directoryInfo.SetAccessControl(dirSecurity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}