Creating XML file with write access given to all the windows account users

后端 未结 3 487
有刺的猬
有刺的猬 2021-01-24 01:19

I am creating an XML file in the common application folder using C#:

%ALLUSERSPROFILE%\\Application Data\\

File will be created when applicatio

相关标签:
3条回答
  • 2021-01-24 01:30

    You can't pass information about ACL into XDocument.Save method, but you can modify permissions of file after saving your xml document. You can use the following code to perform it (don't forget to add reference to System.Security.dll):

    using System.Security.AccessControl;
    using System.Security.Principal;
    using System.IO;
    
    public class FileAccessRulesHelper
    {
        public void AddWriteRightsToEveryone(string filename)
        {
            // get sid of everyone group
            SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            // create rule
            FileSystemAccessRule rule = new FileSystemAccessRule(sid, FileSystemRights.Write, AccessControlType.Allow);
            // get ACL of file
            FileSecurity fsecurity = File.GetAccessControl(filename);
            // modify ACL of file
            fsecurity.AddAccessRule(rule);
            // apply modified ACL to file
            File.SetAccessControl(filename, fsecurity);
        }
    }
    
    0 讨论(0)
  • 2021-01-24 01:43

    I had a similar problem with a service install. You can use the following code to give a folder different permissions.

    public static void CreateWithFullAccess(string targetDirectory)
        {
            try
            {
                if (!Directory.Exists(targetDirectory))
                {
                    Directory.CreateDirectory(targetDirectory);
                }
                DirectoryInfo info = new DirectoryInfo(targetDirectory);
                SecurityIdentifier allUsersSid =
                new SecurityIdentifier(WellKnownSidType.LocalServiceSid,
                null);
                DirectorySecurity security = info.GetAccessControl();
                security.AddAccessRule(
                new FileSystemAccessRule(allUsersSid,
                FileSystemRights.FullControl,
                AccessControlType.Allow));
                info.SetAccessControl(security);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }
    
    0 讨论(0)
  • 2021-01-24 01:45

    I don't think you can pass a parameter to XDocument.Save to control the permissions but you should be able to set them after the save. Something like the following should do:

    System.Security.AccessControl.FileSecurity fsec = System.IO.File.GetAccessControl(fileName);
    fsec.AddAccessRule( new System.Security.AccessControl.FileSystemAccessRule("Everyone", System.Security.AccessControl.FileSystemRights.Modify, System.Security.AccessControl.AccessControlType.Allow));
    System.IO.File.SetAccessControl(fileName, fsec);
    
    0 讨论(0)
提交回复
热议问题