How to set read write permission to directory in C#.NET

While working with files and directory we need read write permission on files and folders, this can be done by manually giving access permission to directory as well as programmatically.

Here is code

Full access control to folder or directory

        private static void SetPermissions(string dirPath)
            DirectoryInfo info = new DirectoryInfo(dirPath);
            WindowsIdentity self = System.Security.Principal.WindowsIdentity.GetCurrent();
            DirectorySecurity ds = info.GetAccessControl();
            ds.AddAccessRule(new FileSystemAccessRule(self.Name, 
            InheritanceFlags.ObjectInherit |

read-only on access on file

    FileInfo myFile = new FileInfo(files.FullName);
    myFile.IsReadOnly = false;


File.SetAttributes(files.FullName, File.GetAttributes(files.FullName) & ~FileAttributes.ReadOnly);

Create a file with Read Write permission

 FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);

This entry was posted in C#.NET and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s