Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have been using ManagementClass to create shares, and they can be seen in with the "net share" command line and by using the computer's IP's address in Windows Explorer. The problem is if you look at the properties of the folder that is being shared you do not see the sharing information in the Directory's Property Window Sharing tab. How to I create a share that can been seen in the there also.

What I have tried:

public static void ShareFolder(string folderPath, string shareName, string description = null)
{
    //First ensure share does not exist
    DeleteShare(shareName);
    try
    {
        ManagementClass managementClass = new ManagementClass("Win32_Share");
        ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");

        // Set the input parameters
        if (description != null)
            inParams["Description"] = description;
        inParams["Name"] = shareName;
        inParams["Path"] = folderPath;
        inParams["Type"] = 0x80000000; // DISK_DRIVE_ADMIN

        //Another Type:
        // DISK_DRIVE = 0x0
        // PRINT_QUEUE = 0x1
        // DEVICE = 0x2
        // IPC = 0x3
        // DISK_DRIVE_ADMIN = 0x80000000
        // PRINT_QUEUE_ADMIN = 0x80000001
        // DEVICE_ADMIN = 0x80000002
        // IPC_ADMIN = 0x8000003
        //inParams["MaximumAllowed"] = int maxConnectionsNum;
        // Invoke the method on the ManagementClass object
        var 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)
    {
        throw new Exception(string.Format("Could not create share \"{0}\" with path {1}", shareName, folderPath), ex);
    }
}
Posted

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