FileIOPermissionAccess.Append

   
 

using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Permissions;
using System.IO;

class Program {
    static void Main(string[] args) {
        CodeAccessPermission permission = new FileIOPermission(FileIOPermissionAccess.Append,@"C:audit.txt");
        permission.Deny();
        AuditClass.Save("some data to audit");
        CodeAccessPermission.RevertDeny();
    }
}

class AuditClass {
    public static void Save(string value) {
        try {
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Append,@"C:audit.txt");
            permission.Assert();
            FileStream stream = new FileStream(@"C:audit.txt",FileMode.Append, FileAccess.Write);
            CodeAccessPermission.RevertAssert();
            Console.WriteLine("Data written to audit file");
        } catch {
            Console.WriteLine("Failed to write data to audit file");
        }
    }
}

    


This entry was posted in Security. Bookmark the permalink.