Monday, June 25, 2012

Shared folder and permission for Everyone via C#.Net

This is again from the tool which we did to verify some installers. Don’t ask why the installer is trying to create some shared folders with permission for Everyone.Oh its not a simple read permission, its FullControl!!! Full control to everyone to do anything inside the folders. There is a famous saying in my mother tongue Malayalam which is “No questions on stories because it’s a story” Smile
We are interested in the technical side of the scenario. Here by I just want to show a sample of how to retrieve the file or folder permissions using C#.Net. Its simple as creating Object of DirectorySecurity and iterating through its properties.
Note that this will work for any kind of folders ie with normal folders such as  “c:\windows\system32\” or shared folders such as “\\localhost\dropbox
private bool CheckFolderPermForEveryone(string sharedFolderPath)
{
    DirectorySecurity dsShared = Directory.GetAccessControl(sharedFolderPath);
    AuthorizationRuleCollection authRules= dsShared.GetAccessRules(true, true, typeof(NTAccount));
    foreach (FileSystemAccessRule rule in authRules)
    {
        if (rule.IdentityReference.Value.Contains("Everyone"))
        {
            if (rule.FileSystemRights==FileSystemRights.FullControl){
                return true;
            }
        }
    }
    return false;
}





Please don’t look at the performance or coding style as this really quick code just to make the tool working.

No comments: