Original URL: | https://blogs.msdn.microsoft.com/alejacma/2008/05/26/who-has-access-to-a-folder-c/ |
Post name: | Who has access to a folder? (C#) |
Original author: | Alejandro Campos Magencio |
Posting date: | 2008-05-26T04:00:00+00:00 |
Hi all, welcome back,
The following .NET 2.0sample shows how to get security info from a folder to find out the permissions for users/groups on it:
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Variables
//
string strFolderPath = "";
DirectoryInfo dirInfo = null;
DirectorySecurity dirSec = null;
int i = 0;try
{
// Read the path to the directory
//
do
{
Console.Write("Enter existing directory > ");
strFolderPath = Console.ReadLine();
} while (!Directory.Exists(strFolderPath));// Get the ACL of the directory
//
dirInfo = new DirectoryInfo(strFolderPath);
dirSec = dirInfo.GetAccessControl();// Show the ACEs of the ACL
//
i = 0;
foreach (FileSystemAccessRule rule in dirSec.GetAccessRules(true, true, typeof(NTAccount)))
{
Console.WriteLine("[{0}] - Rule {1} {2} access to {3}",
i++,
rule.AccessControlType == AccessControlType.Allow ? "grants" : "denies",
rule.FileSystemRights,
rule.IdentityReference.ToString());
}
}
catch (Exception ex)
{
Console.Write("EXCEPTION: ");
Console.WriteLine(ex.Message);
}
Console.WriteLine("\n<
>");
Console.Read();
}
}
}
I hope this helps.
Cheers,
Alex (Alejandro Campos Magencio)
Comments: