Csharp/C Sharp/Security/FileIOPermissionAccess

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

FileIOPermissionAccess.AllAccess

 
using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Permissions;
class Program {
    static void Main(string[] args) {
        CodeAccessPermission permissionA = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\");
        CodeAccessPermission permissionB = new FileIOPermission(FileIOPermissionAccess.Read, @"C:\temp");
        if (permissionB.IsSubsetOf(permissionA)) {
            Console.WriteLine("PermissionB is a subset of PermissionA");
        } else {
            Console.WriteLine("PermissionB is NOT a subset of PermissionA");
        }
    }
}


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");
        }
    }
}