Csharp/C Sharp/Windows/File Security Permission — различия между версиями

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

Текущая версия на 11:43, 26 мая 2010

Demand FileIOPermissionAccess.AllAccess

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
public class MainClass {
    public static void Main() {
        try {
            FileIOPermission fileIOPermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"c:\");
            fileIOPermission.Demand();
        } catch (SecurityException) {
            
        }
    }
}


FileSecurity

using System;
using System.IO;
using System.Security.AccessControl;
class MainClass {
    static void Main(string[] args) {
        FileStream stream;
        string fileName;
        fileName = Path.GetRandomFileName();
        using (stream = new FileStream(fileName, FileMode.Create)) {
            // Do something.
        }
        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);
    }
}


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


Use SecurityManager to check the FileIOPermissionAccess.Write

using System;
using System.Security;
using System.Security.Permissions;
class MainClass {
    public static void Main() {
        FileIOPermission fileIOPerm = new FileIOPermission(FileIOPermissionAccess.Write, @"C:\Data");
        Console.WriteLine(SecurityManager.IsGranted(fileIOPerm));
    }
}