Applying user permissions on
any folder using C#.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Security.AccessControl;
using
System.IO;
namespace
ChangeFolderPermission
{
class Program
{
static void
Main(string[] args)
{
string desktopFolderPath =
@"D:\Temp";
FileSystemAccessRule iis_iusrs =
new
FileSystemAccessRule("IIS_IUSRS",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
FileSystemAccessRule networkService =
new
FileSystemAccessRule("NETWORK SERVICE",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
System.IO.DirectoryInfo
desktopFolderDirectory = new System.IO.DirectoryInfo(desktopFolderPath);
DirectorySecurity directorySecurity =
null;
if (!desktopFolderDirectory.Exists)
{
System.IO.Directory.CreateDirectory(desktopFolderPath);
directorySecurity = desktopFolderDirectory.GetAccessControl();
directorySecurity.AddAccessRule(iis_iusrs);
directorySecurity.AddAccessRule(networkService);
desktopFolderDirectory.SetAccessControl(directorySecurity);
}
else
{
directorySecurity = desktopFolderDirectory.GetAccessControl();
directorySecurity.AddAccessRule(iis_iusrs);
directorySecurity.AddAccessRule(networkService);
desktopFolderDirectory.SetAccessControl(directorySecurity);
}
}
}
}