Set file access rule in CSharp

Description

The following code shows how to set file access rule.

Example


         //w  ww  . j  a  v a  2  s . c  o  m

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;
using System.Security;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        string fileName = "c:\\test.txt";
        FileSecurity acl = new FileSecurity();
        acl.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name,
            FileSystemRights.CreateFiles | FileSystemRights.Modify | FileSystemRights.Delete,
            AccessControlType.Allow));
        using (FileStream file = File.Open(fileName, FileMode.OpenOrCreate))
        {
            
        }
    }
}




















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip