Feedback

C# - Sicherheit von Verzeichnisssen und Dateien ändern

Veröffentlicht von am 23.11.2007
(1 Bewertungen)
Mit dieser Klasse können die Sicherheitseinstellungen von Dateien und Verzeichnissen geändert werden.

z.B.: Benuzterrechte und Gruppenrechte
GFU-Schulungen  [Anzeige]

VB.NET 2017/2015/2013 Einführung

Das Seminar befähigt Sie zur eigenständigen Entwicklung von anwenderorientierten Programmen in VB.NET, worin auch der Einsatz von Datenbanken enthalten ist.

C# Komplett

Sie kennen sich in objektorientierter Programmierung aus. Sie schreiben C++ oder Java? Und nun stehen Sie vor der Aufgabe, in C# Anwendungen zu erstellen. Das C# Komplett-Seminar verschafft Ihnen umfassende Programmierkenntnisse in dieser Sprache. Nach der Schulung entwickeln Sie selbständig Anwendungen mit C#. Sie kennen die Datentypen und Klassenbibliotheken der objektorientierten Programmiersprache C#. Der Komplettkurs setzt bei den Grundlagen von C# ein. Sie arbeiten mit Variablen und konvertieren Typen. Multithreading, Delegates, Generics sind nach dem Seminar für Sie kein Geheimnis mehr.

using System;
using System.Collections.Generic;
using System.Text;

namespace Helper
{
    public class SecurityDirectory
    {
        /// <summary>
        /// Adds an AccessControlList entry on the specified directory for the specified account.
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="identity"></param>
        /// <param name="fileSystemRights"></param>
        /// <param name="inheritanceFlags"></param>
        /// <param name="propogationFlags"></param>
        /// <param name="accessControlType"></param>
        public static void AddDirectorySecurity(string directoryPath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propogationFlags, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Create a new DirectoryInfo object. 
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(directoryPath);

            // Get a DirectorySecurity object that represents the current security settings. 
            System.Security.AccessControl.DirectorySecurity dirSecurity = dirInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.  
            dirSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, inheritanceFlags, propogationFlags, accessControlType));

            // Set the new access settings. 
            dirInfo.SetAccessControl(dirSecurity);
        }
        /// <summary>
        /// Adds an AccessControlList entry on the specified directory for the specified account.
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="identity"></param>
        /// <param name="fileSystemRights"></param>
        /// <param name="accessControlType"></param>
        public static void AddDirectorySecurity(string directoryPath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Create a new DirectoryInfo object. 
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(directoryPath);

            // Get a DirectorySecurity object that represents the current security settings. 
            System.Security.AccessControl.DirectorySecurity dirSecurity = dirInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings.  
            dirSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, accessControlType));

            // Set the new access settings. 
            dirInfo.SetAccessControl(dirSecurity);
        }

        /// <summary>
        /// Removes an AccessControlList entry on the specified directory for the specified account.
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="identity"></param>
        /// <param name="fileSystemRights"></param>
        /// <param name="inheritanceFlags"></param>
        /// <param name="propogationFlags"></param>
        /// <param name="accessControlType"></param>
        public static void RemoveDirectorySecurity(string directoryPath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propogationFlags, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Create a new DirectoryInfo object. 
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(directoryPath);

            // Get a DirectorySecurity object that represents the current security settings. 
            System.Security.AccessControl.DirectorySecurity dirSecurity = dirInfo.GetAccessControl();

            // Remove the FileSystemAccessRule from the security settings.  
            dirSecurity.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, inheritanceFlags, propogationFlags, accessControlType)); 

            // Set the new access settings. 
            dirInfo.SetAccessControl(dirSecurity);
        }
        /// <summary>
        /// Removes an AccessControlList entry on the specified directory for the specified account.
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="identity"></param>
        /// <param name="fileSystemRights"></param>
        /// <param name="accessControlType"></param>
        public static void RemoveDirectorySecurity(string directoryPath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Create a new DirectoryInfo object. 
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(directoryPath);

            // Get a DirectorySecurity object that represents the current security settings. 
            System.Security.AccessControl.DirectorySecurity dirSecurity = dirInfo.GetAccessControl();

            // Remove the FileSystemAccessRule from the security settings.  
            dirSecurity.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, accessControlType));

            // Set the new access settings. 
            dirInfo.SetAccessControl(dirSecurity);
        }

        /// <summary>
        /// Removes an AccessControlList entry on the specified directory for the specified account. 
        /// </summary>
        /// <param name="directoryPath"></param>
        public static void RemoveInheritablePermissons(string directoryPath)
        {
            // Create a new DirectoryInfo object. 
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(directoryPath);

            // Get a DirectorySecurity object that represents the current security settings. 
            System.Security.AccessControl.DirectorySecurity dirSecurity = dirInfo.GetAccessControl();

            // Add the FileSystemAccessRule to the security settings. 
            const bool IsProtected = true;
            const bool PreserveInheritance = false;
            dirSecurity.SetAccessRuleProtection(IsProtected, PreserveInheritance);

            // Set the new access settings. 
            dirInfo.SetAccessControl(dirSecurity);
        }

    }
    class SecurityFile
    {
        /// <summary>
        /// Adds an AccessControlList entry on the specified file for the specified account.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="identity"></param>
        /// <param name="fileSystemRights"></param>
        /// <param name="inheritanceFlags"></param>
        /// <param name="propogationFlags"></param>
        /// <param name="accessControlType"></param>
        public static void AddFileSecurity(string filePath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propogationFlags, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Get a FileSecurity object that represents the current security settings.
            System.Security.AccessControl.FileSecurity fileSecurity = System.IO.File.GetAccessControl(filePath);

            // Add the FileSystemAccessRule to the security settings. 
            fileSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, inheritanceFlags, propogationFlags, accessControlType)); 

            // Set the new access settings.
            System.IO.File.SetAccessControl(filePath, fileSecurity);
        }
       /// <summary>
        /// Adds an AccessControlList entry on the specified file for the specified account.
       /// </summary>
       /// <param name="filePath"></param>
       /// <param name="identity"></param>
       /// <param name="fileSystemRights"></param>
       /// <param name="accessControlType"></param>
        public static void AddFileSecurity(string filePath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Get a FileSecurity object that represents the current security settings.
            System.Security.AccessControl.FileSecurity fileSecurity = System.IO.File.GetAccessControl(filePath);

            // Add the FileSystemAccessRule to the security settings. 
            fileSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, accessControlType));

            // Set the new access settings.
            System.IO.File.SetAccessControl(filePath, fileSecurity);
        }
        
        /// <summary>
        /// Removes an AccessControlList entry on the specified file for the specified account.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="identity"></param>
        /// <param name="fileSystemRights"></param>
        /// <param name="inheritanceFlags"></param>
        /// <param name="propogationFlags"></param>
        /// <param name="accessControlType"></param>
        public static void RemoveFileSecurity(string filePath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.InheritanceFlags inheritanceFlags, System.Security.AccessControl.PropagationFlags propogationFlags, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Get a FileSecurity object that represents the current security settings.
            System.Security.AccessControl.FileSecurity fileSecurity = System.IO.File.GetAccessControl(filePath);

            // Add the FileSystemAccessRule to the security settings. 
            fileSecurity.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, inheritanceFlags, propogationFlags, accessControlType)); 

            // Set the new access settings.
            System.IO.File.SetAccessControl(filePath, fileSecurity);
        }

        /// <summary>
        /// Removes an AccessControlList entry on the specified file for the specified account.
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="identity"></param>
        /// <param name="fileSystemRights"></param>
        /// <param name="accessControlType"></param>
        public static void RemoveFileSecurity(string filePath, string identity, System.Security.AccessControl.FileSystemRights fileSystemRights, System.Security.AccessControl.AccessControlType accessControlType)
        {
            // Get a FileSecurity object that represents the current security settings.
            System.Security.AccessControl.FileSecurity fileSecurity = System.IO.File.GetAccessControl(filePath);

            // Add the FileSystemAccessRule to the security settings. 
            fileSecurity.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(identity, fileSystemRights, accessControlType));

            // Set the new access settings.
            System.IO.File.SetAccessControl(filePath, fileSecurity);
        }

        /// <summary>
        /// Removes an AccessControlList entry on the specified directory for the specified account. 
        /// </summary>
        /// <param name="fileName">Path to the folder</param> 
        public static void RemoveInheritablePermissons(string fileName)
        {
            // Get a DirectorySecurity object that represents the current security settings. 
            System.Security.AccessControl.FileSecurity fileSecurity = System.IO.File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings. 
            const bool IsProtected = true;
            const bool PreserveInheritance = false;
            fileSecurity.SetAccessRuleProtection(IsProtected, PreserveInheritance);

            // Set the new access settings. 
            System.IO.File.SetAccessControl(fileName,fileSecurity);
        }
    }
}

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!