Naveen Soni Naveen Soni - 1 month ago 16
C# Question

Group Permission for Directory in C#

I am using console application visual studio 2012 for make permission of group for directory but is getting error "Some or all identity references could not be translated."

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

}

Answer

I have got the solution of my issue.

I dont had Active Directory Group in my local. DirectorySecurity, AddAccessRule will work only Active Directory Group, so on client server I execute the console application from this sql side with this keyword: "xp_cmdshell", Then a directory have created in Active Directory Group and that time not getting ant type of error.