Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to get all the access permissions of a shared folder ( note : please note that this is not security permissions )

But i can able to get all the security rules of the folder.

What I have tried:

I have tried below codes:

var directoryInfo = new DirectoryInfo(sharedFolderPath);
                var directorySecurity = directoryInfo.GetAccessControl();
                var currentUserIdentity = WindowsIdentity.GetCurrent();

                foreach (FileSystemAccessRule rule in directoryInfo.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)))
                {

                    
                }


When i try to view this foreach all items . I get all the security rules rather than share folder permissions list



Can anyone help me on this
Posted
Updated 25-May-18 5:52am

1 solution

I found this code with a simple google search.

C#
using System;
   using System.Management;

   ...

   private static void ShareSecurity(string ServerName)
   {
       ConnectionOptions myConnectionOptions = new  ConnectionOptions();

       myConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;
       myConnectionOptions.Authentication = AuthenticationLevel.Packet;

       ManagementScope myManagementScope =
           new ManagementScope(@"\\" + ServerName + @"\root\cimv2", myConnectionOptions);

       myManagementScope.Connect();

       if (!myManagementScope.IsConnected)
           Console.WriteLine("could not connect");
       else
       {
           ManagementObjectSearcher myObjectSearcher =
               new ManagementObjectSearcher(myManagementScope.Path.ToString(), "SELECT * FROM Win32_LogicalShareSecuritySetting");

           foreach(ManagementObject share in myObjectSearcher.Get())
           {
               Console.WriteLine(share["Name"] as string);
               InvokeMethodOptions options = new InvokeMethodOptions();
               ManagementBaseObject outParamsMthd = share.InvokeMethod("GetSecurityDescriptor", null, options);
               ManagementBaseObject descriptor = outParamsMthd["Descriptor"] as ManagementBaseObject;
               ManagementBaseObject[] dacl =  descriptor["DACL"] as ManagementBaseObject[];

               foreach (ManagementBaseObject ace in dacl)
               {
                   try
                   {
                       ManagementBaseObject trustee = ace["Trustee"] as ManagementBaseObject;
                       Console.WriteLine(
                           trustee["Domain"] as string + @"\" + trustee["Name"] as string + ": " +
                           ace["AccessMask"] as string + " " + ace["AceType"] as string
                       );
                   }
                   catch (Exception error)
                   {
                       Console.WriteLine("Error: "+ error.ToString());
                   }
               }
           }
       }
   }
 
Share this answer
 
Comments
Greek Varak 28-May-18 4:09am    
Hi John Simmons . Thank you so much it can get the list.
Is it possible to delete or add user permissions from here ?
#realJSOP 28-May-18 5:44am    
My guess would be that you need "full control" permissions on the shared folder in question.

The API you're interested in would probably include Directory.SetAccessControl.

Once again, google is your friend.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900