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