FileSecurity.ResetAccessRule : FileSecurity « System.Security.AccessControl « C# / C Sharp by API






FileSecurity.ResetAccessRule

  

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

static class MainClass
{
    static void Main(string[] args)
    {
        FileStream stream = null;
        string fileName = "c:\\test.txt";

        // Deny 'Everyone' access to the file
        FileSecurity fSecurity = File.GetAccessControl(fileName);
        fSecurity.ResetAccessRule(new FileSystemAccessRule("Everyone",FileSystemRights.Read, AccessControlType.Deny));
        File.SetAccessControl(fileName, fSecurity);

        // Attempt to access file.
        try
        {
            stream = new FileStream(fileName, FileMode.Create);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception thrown: ");
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            stream.Close();
            stream.Dispose();
        }
    }
}

   
    
  








Related examples in the same category

1.FileSecurity.AddAccessRule