Code: |
using System;
using System.IO;
using System.Security.Principal;
using System.Security.AccessControl;
class ChangeAccess
{
public static void Main()
{
string __StartPath = "C:\\$Recycle.Bin";
string[] __Directories;
string __UserName = string.Empty;
FileSystemAccessRule __AllowRule, __DenyRule;
bool __Modified = false;
FileSecurity __FileSec;
Console.WriteLine("Getting the current user name");
try
{
__UserName = WindowsIdentity.GetCurrent().Name.ToString();
}
catch (System.Security.SecurityException e)
{
Console.WriteLine("Failed getting the current user name: {0}\nPress any key for exit", e);
Console.Read();
}
finally
{
__DenyRule = new FileSystemAccessRule(__UserName, FileSystemRights.FullControl, AccessControlType.Deny);
__AllowRule = new FileSystemAccessRule(__UserName, FileSystemRights.FullControl, AccessControlType.Allow);
}
Console.WriteLine("The current user name is: {0}\nGetting the current access control of path: {1}", __UserName, __StartPath);
__FileSec = File.GetAccessControl(__StartPath);
Console.WriteLine("Removing the current access rules");
__FileSec.RemoveAccessRule(__AllowRule);
__FileSec.RemoveAccessRule(__DenyRule);
Console.WriteLine("Changing the current access rules");
__FileSec.ModifyAccessRule(AccessControlModification.Set, __AllowRule, out __Modified);
Console.WriteLine("Applying the new entries of control access to current directory");
File.SetAccessControl(__StartPath, __FileSec);
Console.WriteLine("Getting the current list of subdirectories in: {0}", __StartPath);
__Directories = Directory.GetDirectories(__StartPath, "*", SearchOption.AllDirectories);
Console.WriteLine("{0} current directories in: {1} :", __Directories.Length.ToString(), __StartPath);
foreach (string __Current in __Directories)
Console.WriteLine(__Current);
Console.WriteLine("Finished!");
Console.ReadLine();
}
} |
_________________
Excuse me if you don't understand what I just said, but "english" isn't my native language.