Sunday, January 31, 2010

Setting permission to the folder programmatically using C#.Net

using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;



string dirpath = "C:\\Web\\DirPath";
DirectoryInfo dirinfo = new DirectoryInfo(dirpath);
string users = @"NETWORK SERVICE"; //@"ASPNET" // @"EVERYONE"
if (dirinfo.Exists)
{
DirectorySecurity dirsec = dirinfo.GetAccessControl();
FileSystemAccessRule fsar = new FileSystemAccessRule(new NTAccount(users), FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
dirsec.AddAccessRule(fsar);
dirinfo.SetAccessControl(dirsec);
}

No comments:

Post a Comment