1 ) Set Access Control
DirectoryInfo dInfo = new DirectoryInfo(fileName); DirectorySecurity dSecurity = dInfo.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule("everyone",FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,PropagationFlags.InheritOnly,AccessControlType.Allow)); dInfo.SetAccessControl(dSecurity);
2) Sharing foldel
ManagementClass mc = new ManagementClass("win32_share"); ManagementBaseObject inParams = mc.GetMethodParameters("Create"); inParams("Description") = "My Shared Folder"; inParams("Name") = "Shared Folder Name"; inParams("Path") = "C:\Folder1"; inParams("Type") = ShareResourceType.DiskDrive; inParams("MaximumAllowed") = null; inParams("Password") = null; inParams("Access") = null; // Make Everyone has full control access. ManagementBaseObject outParams = classObj.InvokeMethod("Create", inParams, null);
3) Only in Windows 7 and Vista, upgrade "Everyone" sharing right
//user selection NTAccount ntAccount = new NTAccount("Everyone"); //SID SecurityIdentifier userSID = (SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier)); byte[] utenteSIDArray = new byte[userSID.BinaryLength]; userSID.GetBinaryForm(utenteSIDArray, 0); //Trustee ManagementObject userTrustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null); userTrustee["Name"] = "Everyone"; userTrustee["SID"] = utenteSIDArray; //ACE ManagementObject userACE = new ManagementClass(new ManagementPath("Win32_Ace"), null); userACE["AccessMask"] = 2032127; //Full access userACE["AceFlags"] = AceFlags.ObjectInherit | AceFlags.ContainerInherit; userACE["AceType"] = AceType.AccessAllowed; userACE["Trustee"] = userTrustee; ManagementObject userSecurityDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null); userSecurityDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT userSecurityDescriptor["DACL"] = new object[] { userACE }; //UPGRADE SECURITY PERMISSION ManagementClass mc = new ManagementClass("Win32_Share"); ManagementObject share = new ManagementObject(mc.Path + ".Name='" + CondivisionName + "'"); share.InvokeMethod("SetShareInfo", new object[] { Int32.MaxValue, description, userSecurityDescriptor });
This allow me to upgrade the security permission of "Everyone" in Windows Vista & 7 and get "Full Control".
/* * This method is used to perform the main actions of sharing the folders * It accepts three arguments: - * A path of the folder, * A ShareName by which you would want to share the folder * Description of the folder * You cannot have the first two arguments as empty. They should consist of * data. The third arguments can be an empty string. */ private static void QshareFolder(string FolderPath, string ShareName, string Description) { try { // Create a ManagementClass object ManagementClass managementClass = new ManagementClass("Win32_Share"); // Create ManagementBaseObjects for in and out parameters ManagementBaseObject inParams = managementClass.GetMethodParameters("Create"); ManagementBaseObject outParams; // Set the input parameters inParams["Description"] = Description; inParams["Name"] = ShareName; inParams["Path"] = FolderPath; inParams["Type"] = 0x0; // Disk Drive //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"] = null; inParams["Password"] = null; inParams["Access"] = null; // Make Everyone has full control access. //inParams["MaximumAllowed"] = int maxConnectionsNum; // 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) { //MessageBox.Show ("Unable to share the folder: " + outParams.Properties["ReturnValue"].Value); MessageBox.Show("Error sharing MMS folders. Please make sure you install as Administrator.", "Error!"); } //user selection NTAccount ntAccount = new NTAccount("Everyone"); //SID SecurityIdentifier userSID = (SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier)); byte[] utenteSIDArray = new byte[userSID.BinaryLength]; userSID.GetBinaryForm(utenteSIDArray, 0); //Trustee ManagementObject userTrustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null); userTrustee["Name"] = "Everyone"; userTrustee["SID"] = utenteSIDArray; //ACE ManagementObject userACE = new ManagementClass(new ManagementPath("Win32_Ace"), null); userACE["AccessMask"] = 2032127; //Full access userACE["AceFlags"] = AceFlags.ObjectInherit | AceFlags.ContainerInherit; userACE["AceType"] = AceType.AccessAllowed; userACE["Trustee"] = userTrustee; ManagementObject userSecurityDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null); userSecurityDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT userSecurityDescriptor["DACL"] = new object[] { userACE }; //can declare share either way, where "ShareName" is the name used to share the folder //ManagementPath path = new ManagementPath("Win32_Share.Name='" + ShareName + "'"); //ManagementObject share = new ManagementObject(path); ManagementObject share = new ManagementObject(managementClass.Path + ".Name='" + ShareName + "'"); share.InvokeMethod("SetShareInfo", new object[] { Int32.MaxValue, Description, userSecurityDescriptor }); } catch (Exception ex) { MessageBox.Show("Error sharing folders. Please make sure you install as Administrator. ERROR: " + ex.Message, "Error!"); } } /* * this method is called as soon the app is started. * this method gets the current directory of the application, * calls another method which creates and shares the apps and logs folders * under the current directory. */ private static void Share() { string strfol; string folder; string newFolder; DirectoryInfo di; try { strfol = Application.ExecutablePath; strfol = strfol.Substring(0,strfol.IndexOf(Application.ProductName + ".EXE")); newFolder = "apps"; folder = strfol + newFolder; if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); di = new DirectoryInfo(folder); DirectorySecurity dSecurity = di.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); di.SetAccessControl(dSecurity); if (di != null) QshareFolder(folder, newFolder, ""); } catch (Exception ex) { MessageBox.Show("Error sharing folders. Please make sure you install as Administrator. ERROR: " + ex.Message, "Error!"); } }