Add users and permission to directory and files by using c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace WinTest
{
   
public partial class PermissionTest : Form
   
{
       
public PermissionTest()
       
{
           
InitializeComponent();
        
}

       
private void PermissionTest_Load(object sender, EventArgs e)
       
{
           
string dirName1 = @"c:\Test1";
           
//string userName = System.Environment.MachineName +@"\Network Service";
            
//string userName = System.Environment.MachineName +@"\ASPNET";
            
string userName = "NETWORK SERVICE";
           
DirectoryInfo dInfo1 = new DirectoryInfo(dirName1);
           
DirectorySecurity dSecurity1 = dInfo1.GetAccessControl();
           
dSecurity1.AddAccessRule(new FileSystemAccessRule(userName, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
           
dInfo1.SetAccessControl(dSecurity1);
       
}
    }
}

Ebook Download
View all
Learn
View all