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); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)