how to give permission to file rather than directory

user584018

I'm using below code to give permission to a directory

static void SetPermission(string path)
    {
        if (Directory.Exists(path))
        {
            var directoryInfo = new DirectoryInfo(path);

            // get the ACL of the directory
            var directorySecurity = directoryInfo.GetAccessControl();

            // remove inheritance, copying all entries so that they are direct ACEs
            directorySecurity.SetAccessRuleProtection(true, true);

            // do the operation on the directory
            directoryInfo.SetAccessControl(directorySecurity);

            // re-read the ACL
            directorySecurity = directoryInfo.GetAccessControl();

            // get the well known SID for "Users"
            var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);

            // loop through every ACE in the ACL
            foreach (FileSystemAccessRule rule in directorySecurity.GetAccessRules(true, false, typeof(SecurityIdentifier)))
            {
                // if the current entry is one with the identity of "Users", remove it
                if (rule.IdentityReference == sid)
                    directorySecurity.RemoveAccessRule(rule);
            }

            var ntVirtaulUserName = @"NT Service\ServiceName";

            // Add the FileSystemAccessRule to the security settings. give full control for user 'NT Service\ServiceName' 
            directorySecurity.AddAccessRule(new FileSystemAccessRule(ntVirtaulUserName.Replace(@".\", ""), FileSystemRights.FullControl, AccessControlType.Allow));

            // do the operation on the directory
            directoryInfo.SetAccessControl(directorySecurity);
        }
    }

This is working when giving permission to a directory (Test),

SetPermission(@"C:\Test");

Now I would like to give permission a file under the Test directory (log.txt), how to do that?

Hadi Mohammadi

You can use FileInfo Class to handle permission on files. It's usage is like DirectoryInfo. Here is Microsoft document.

    public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
    {

        FileInfo fInfo = new FileInfo(FileName);
        FileSecurity fSecurity = fInfo.GetAccessControl();

        fSecurity.AddAccessRule(newFileSystemAccessRule(Account,Rights,ControlType));
        fInfo.SetAccessControl(fSecurity);

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to give file permission to a specific user in a Group?

How do I give write permission to file in Linux?

Is it possible to give JCMD a service name rather than PID?

In IPython, how do you save and append to a file rather than overwriting it?

How to get Exif data from an InputStream rather than a File?

Gnuplot scripts: use a path relative to file rather than execution directory?

AVAssetReader, how to use with a stream rather than a file?

How to import index.vue by just mentioning the parent directory name rather than filename?

How to download file rather than HTML with requests

How to lemmatize a .txt file rather than a sentence with pywsd.utils?

How to give permission to create directory with c#

create file in directory without x permission set on directory : How?

How to give directory and file permissions to a web server

How to give apache permission to write to home directory?

How to download docx file rather than loading file in Firefox

Execute file rather than parse it

Give web sever directory full permission

How can I pass in text rather than a file as a variable

How to copy files within directory rather than a whole directory in TCL?

Browserify a library rather than a file

Terminals opening in root directory rather than $HOME

Why is the current directory in the output rather than the directory of the file?

How to give a permission to create log file in systemd service?

What does "omitting directory" mean and how do I make it cp the directory rather than omit it

In Unix ,If we list the no of files in a directory . if no file is found how to get only the output rather than the error message

GCP cloud functions with "gcloud functions deploy" fails with --source as file rather than directory

Using the Files.move creates a new "file" file type rather than moving the file to a directory

Is there a way to put every configuration file to a config directory rather than root in Node project?

How can I create a text file inside a specific directory? It is currently creating it next to the directory rather than inside it