Read C # Access Permissions

Can I read the sharing permissions assigned to a shared folder? I can read in programmaticaly local security settings (those under the right mouse button> Properties> Security), without any problems. But I wonder how I can read permissions in the Right-Click> Sharing and Security ... section> Permissions

Here is the permissions image I want to read:

Share permissions

Is it possible? I am using an XP Pro computer if this helps.

Edit:

In accordance with my answer, I managed to iterate over all shared resources and gain access to (that is, the person who runs the program) on this resource, but did not find a way to read other rights to this share. This was done using the Win32_Share class , but it does not have the ability to obtain permissions to access other users. If anyone has any helpful tips that will be a huge help.

+3
source share
3 answers

I managed to get this to work by extending the approach used by Petey B. Also, make sure that the process that runs this code represents the privileged user on the server.

    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());
                    }
                }
            }               
        }
    }
+7
source

, , - , .

ManagementClass manClass = new ManagementClass(@"\\" +computerName +@"\root\cimv2:Win32_Share"); //get shares

//run through all the shares
foreach (ManagementObject objShare in manClass.GetInstances())
{
  //ignore system shares
  if (!objShare.Properties["Name"].Value.ToString().Contains('$'))
  {
    //print out the share name and location
    textBox2.Text += String.Format("Share Name: {0}  Share Location: {1}", objShare.Properties["Name"].Value, objShare.Properties["Path"].Value) + "\n";

    Int32 permissions = 0;

    try
    {
      //get the access values you have
      ManagementBaseObject result = objShare.InvokeMethod("GetAccessMask", null, null);

      //value meanings: http://msdn.microsoft.com/en-us/library/aa390438(v=vs.85).aspx
      permissions = Convert.ToInt32(result.Properties["ReturnValue"].Value);
    }
    catch (ManagementException me)
    {
      permissions = -1; //no permissions are set on the share
    }

    textBox2.Text += "You have permissions: " + permissions + "\n\n";

  }
}

- , , , .

+2
source

All Articles