1

I am trying via. C# to change the permission for files. I get the error message despite correct "using" references. What do I need to do to make this work. I also tried to Nuget following Packeges: System.IO.FileSystem.AccessControl and System.Security.AccessControl. But didn't worked.

My code looks like this:

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string fileName = "test.xml";

                Console.WriteLine("Adding access control entry for "
                    + fileName);

                // Add the access control entry to the file.
                AddFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from "
                    + fileName);

                // Remove the access control entry from the file.
                RemoveFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        // Adds an ACL entry on the specified file for the specified account.
        public static void AddFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);
        }

        // Removes an ACL entry on the specified file for the specified account.
        public static void RemoveFileSecurity(string fileName, string account,
            FileSystemRights rights, AccessControlType controlType)
        {

            // Get a FileSecurity object that represents the
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Remove the FileSystemAccessRule from the security settings.
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);
        }
    }
}
Katzer
  • 19
  • 2

2 Answers2

0

Solved it, works fine:

// Adds an ACL entry on the specified file for the specified account.
public static void AddFileSecurity(string fileName, string account,
    FileSystemRights rights, AccessControlType controlType)
{
    var fi1 = new FileInfo(fileName);
    var fsecurity = FileSystemAclExtensions.GetAccessControl(fi1);
   
   // Add the FileSystemAccessRule to the security settings.
   fsecurity.AddAccessRule(new FileSystemAccessRule(account,
       rights, controlType));

    // Set the new access settings.
    FileSystemAclExtensions.SetAccessControl(fi1, fsecurity);
}
Katzer
  • 19
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '22 at 14:59
0

Work by this

Need PackageReference "System.IO.FileSystem.AccessControl" in NetCore

        public static void changeFileAccess(String destination)
        {
            FileSecurity securite = new FileSecurity();
            securite.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.Write | FileSystemRights.Read, AccessControlType.Allow));
#if NETSTANDARD || NETCOREAPP
            FileInfo file = new FileInfo(destination);
            FileSystemAclExtensions.SetAccessControl(file, securite);
#else
            File.SetAccessControl(destination, securite);
#endif
        }
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '23 at 01:22