public void CreateSharedDirectory() { string DirPath = Environment.CurrentDirectory; try { Directory.CreateDirectory(DirPath + "\\TestSharedFolder"); DirectoryInfo dInfo = new DirectoryInfo(DirPath + "\\TestSharedFolder"); DirectorySecurity dSecurity = dInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. dSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); dSecurity.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, AccessControlType.Allow)); // Set the new access settings. NETWORK SERVICE dInfo.SetAccessControl(dSecurity); ///////////////////////////////////////////////////////////////////////////////////////////////// ManagementClass management = new ManagementClass("Win32_Share"); ManagementBaseObject input = management.GetMethodParameters("Create"); ManagementBaseObject output; input["Description"] = "TestSharedFolder"; input["Name"] = "TestSharedFolder"; input["Path"] = DirPath + "\\TestSharedFolder"; input["Type"] = 0x0; input["Access"] = null; output = management.InvokeMethod("Create", input, null); if ((uint)(output.Properties["ReturnValue"].Value) != 0) { throw new Exception("Unable To Share The Directory"); } else { Console.Write("Directory Successfully Shared"); } } catch (Exception e) { Console.Write(e.Message); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)