?
Solved

Get file/folder permissions using .net 2.0 problem

Posted on 2007-08-08
7
Medium Priority
?
1,691 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:nahumba
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 19659536
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
}
0
 

Author Comment

by:nahumba
ID: 19659944
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
0
 
LVL 19

Expert Comment

by:drichards
ID: 19661555
You have most of the answer already, it seems, since you are already using GetAccessRules.  Here is code from another post (http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22675356.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
                ...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:nahumba
ID: 19661727
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!
0
 
LVL 19

Expert Comment

by:drichards
ID: 19661867
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()
0
 
LVL 19

Accepted Solution

by:
drichards earned 2000 total points
ID: 19661881
Here's a trivial example:

        Dim names() As String = {"One", "Two", "Three"}
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
        Dim sep As String = ""
        For Each n As String In names
            sb.Append(sep)
            sb.Append(n)
            sep = ", "
        Next
        Console.WriteLine(sb)
0
 

Author Comment

by:nahumba
ID: 19663372
Thank you drichards!

I really appreciate your solution!


Best Regards,
nahumba
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question