Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding “Everyone” privilege to a network share for a folder

NOTE: Please don't disregard based on the title being similar to others.

I'm trying to share a folder on a Windows 7 machine. And I want to give everyone full permissions to it via C#.

I've seen several articles on other pages including here, that tell how to do it. But like some others, it doesn’t work for me. Below is a snippet taken from SO.

    DirectorySecurity sec = Directory.GetAccessControl(path);
    // Using this instead of the "Everyone" string means we work on non-English systems.
    SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
    sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.FullControl | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
    Directory.SetAccessControl(path, sec);

Sharing the folder is already done before I invoke the code above. The below images are the results of what i get:

enter image description here

So far, so good. But on the next image you'll see that the the two remaining checkboxs are still unchecked.

enter image description here

What am I missing please?

Thanks!

EDIT: Below is the code used to do the actual sharing.

    private static void QshareFolder(string FolderPath, string ShareName, string Description)
    {
        try
        {
            ManagementClass managementClass = new ManagementClass("Win32_Share");
            ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
            ManagementBaseObject outParams;

            inParams["Description"] = Description;
            inParams["Name"] = ShareName;
            inParams["Path"] = FolderPath;
            inParams["MaximumAllowed"] = null;
            inParams["Password"] = null;
            inParams["Access"] = null;
            inParams["Type"] = 0x0; // Disk Drive

            // Invoke the method on the ManagementClass object
            outParams = managementClass.InvokeMethod("Create", inParams, null);

            // Check to see if the method invocation was successful
            if ((uint) (outParams.Properties["ReturnValue"].Value) != 0)
            {
                throw new Exception("Unable to share directory.");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "error!");
        }
    }
like image 359
JimDel Avatar asked Feb 25 '26 14:02

JimDel


1 Answers

Permissions on share and underlying folder are separate - your code set ACL on files/folders... So you are missing portion of setting ACL on network share itself.

One gets minimum between permissions on file and share when finally accessing file via share.

I don't know how to set ACL on share but here is a related C++ question that may be good staring point on how to set permissions on shares: How to create read-only network share programmatically?.

like image 126
Alexei Levenkov Avatar answered Feb 28 '26 04:02

Alexei Levenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!