FileSystemRights.Read : File Security Permission : Windows C# Source Code


Custom Search

C# Source Code » Windows » File Security Permission »

 

FileSystemRights.Read








    



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

class MainClass {
    static void Main(string[] args) {
        FileStream stream;
        string fileName = Path.GetRandomFileName();
        using (stream = new FileStream(fileName, FileMode.Create)) {
        }
        SetRule(fileName, "Everyone", FileSystemRights.Read, AccessControlType.Deny);

        try {
            stream = new FileStream(fileName, FileMode.Create);
        } catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        } finally {
            stream.Close();
            stream.Dispose();
        }
    }

    static void AddRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType) {
        FileSecurity fSecurity = File.GetAccessControl(filePath);
        fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType));
        File.SetAccessControl(filePath, fSecurity);
    }
    static void SetRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType) {
        FileSecurity fSecurity = File.GetAccessControl(filePath);
        fSecurity.ResetAccessRule(new FileSystemAccessRule(account, rights, controlType));
        File.SetAccessControl(filePath, fSecurity);
    }
}
           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Windows
» File Security Permission