Link to home
Start Free TrialLog in
Avatar of nahumba
nahumba

asked on

Get file/folder permissions using .net 2.0 problem

Hi,
I'm trying to get file/folder permissions. I've managed to do that with the
"GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))", except one thing that bothers me. For some permissions, I get "Modify".

As I read on MSDN, I found out that the "modify" permission includes several permissions inside of it.

How can I list ALL permissions, and not get the "modify" permission?

Hope you will be able help me out!
* writing in vb.net, c# example will also be ok.
Avatar of drichards
drichards

You can use tests like (note that Read is also a combinastion value):

                If ((frule.FileSystemRights And FileSystemRights.Read) = FileSystemRights.Read) Then
                    System.Console.Write("read|")
                End If

to test for individual rights. The enumeration looks like this.  You can see which ones are basic and which ones are combination values.

[Flags]
public enum FileSystemRights
{
    AppendData = 4,
    ChangePermissions = 0x40000,
    CreateDirectories = 4,
    CreateFiles = 2,
    Delete = 0x10000,
    DeleteSubdirectoriesAndFiles = 0x40,
    ExecuteFile = 0x20,
    FullControl = 0x1f01ff,
    ListDirectory = 1,
    Modify = 0x301bf,
    Read = 0x20089,
    ReadAndExecute = 0x200a9,
    ReadAttributes = 0x80,
    ReadData = 1,
    ReadExtendedAttributes = 8,
    ReadPermissions = 0x20000,
    Synchronize = 0x100000,
    TakeOwnership = 0x80000,
    Traverse = 0x20,
    Write = 0x116,
    WriteAttributes = 0x100,
    WriteData = 2,
    WriteExtendedAttributes = 0x10
}
Avatar of nahumba

ASKER

Hi, thank you for your reply. Seems that this is what im looking for... just small question: What is the public enum - how do I use it to get the file/folder rights?

Thanks again
You have most of the answer already, it seems, since you are already using GetAccessRules.  Here is code from another post (https://www.experts-exchange.com/questions/22675356/Security-Info-on-a-Directory.html) slightly modified for the problem that was identified towards the bottom of that discussion.  This lets you test for individual access values.  The "ToString" on frule.FileSystemRights only shows you the aggregate permission.  The conditional statements check each individual enum value.  The enum is part of .NET framework - I used the reflector tool to list it out so you can see the numeric values.

            Dim col As AuthorizationRuleCollection = sec.GetAccessRules(True, True, Type.GetType("System.Security.Principal.NTAccount"))
            For Each rule As AuthorizationRule In col
                Dim frule As FileSystemAccessRule = CType(rule, FileSystemAccessRule)
                System.Console.WriteLine(rule.IdentityReference.Value)
                System.Console.WriteLine(frule.AccessControlType.ToString())
                System.Console.WriteLine(frule.FileSystemRights.ToString())
                If (frule.FileSystemRights And FileSystemRights.FullControl) = FileSystemRights.FullControl Then
                    System.Console.Write("full|")
                End If
                If ((frule.FileSystemRights And FileSystemRights.Read) = FileSystemRights.Read) Then
                    System.Console.Write("read|")
                End If
                ...
Avatar of nahumba

ASKER

Thank you very much drichards, I really appreciate your help!

Just one very small question before closing this session:

I want to build a string of the access control values. something like: "Read, List Directory, Create Files"

My question is, what is the most efficient and fastest way to determine of my string is empty? I need to determine this because of the comma character.

In case my string is empty then it should start without the comma (", "), in case it already contain something inside of it - then the comma should be added.

I know how to do this, but because there are many values here, I wanted to know if you could suggest a more efficient way to building this string.



Many thanks!
Make sure you use a StringBuilder as it will be very inefficient to build a potentially long string by concatenating string objects.  I would buld the string like this:


        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
        ' if using a loop ...
        Dim sep As String = ""
        For (whatever your loop is...)
            sb.Append(sep)
            sb.Append(n)
            sep = ", "
        Next

        ' if not using a loop
        sep = ", "
        sb.Append(firstString)
        sb.Append(sep)
        sb.Append(nextString)
        sb.Append(sep)
        sb.Append(nextString)
        ...

        Dim finalString as String = sb.ToString()
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nahumba

ASKER

Thank you drichards!

I really appreciate your solution!


Best Regards,
nahumba