zoukankan      html  css  js  c++  java
  • notecore设置linux/Unix系统文件权限

    一、使用Mono.Posix 包实现

    var unixFileInfo = new Mono.Unix.UnixFileInfo("test.txt");
        // set file permission to 644
        unixFileInfo.FileAccessPermissions =
            FileAccessPermissions.UserRead | FileAccessPermissions.UserWrite
            | FileAccessPermissions.GroupRead
            | FileAccessPermissions.OtherRead;

    二、使用本地代码

    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using static System.Console;
    
    class Program
    {
        [DllImport("libc", SetLastError = true)]
        private static extern int chmod(string pathname, int mode);
    
        // user permissions
        const int S_IRUSR = 0x100;
        const int S_IWUSR = 0x80;
        const int S_IXUSR = 0x40;
    
        // group permission
        const int S_IRGRP = 0x20;
        const int S_IWGRP = 0x10;
        const int S_IXGRP = 0x8;
    
        // other permissions
        const int S_IROTH = 0x4;
        const int S_IWOTH = 0x2;
        const int S_IXOTH = 0x1;
    
        static void Main(string[] args)
        {
            WriteLine("Setting permissions to 0755 on test.sh");
            const int _0755 =
                S_IRUSR | S_IXUSR | S_IWUSR
                | S_IRGRP | S_IXGRP
                | S_IROTH | S_IXOTH;
            WriteLine("Result = " + chmod(Path.GetFullPath("test.sh"), (int)_0755));
    
            WriteLine("Setting permissions to 0644 on sample.txt");
            const int _0644 =
                S_IRUSR | S_IWUSR
                | S_IRGRP
                | S_IROTH;
            WriteLine("Result = " + chmod(Path.GetFullPath("sample.txt"), _0644));
    
            WriteLine("Setting permissions to 0600 on secret.txt");
            const int _0600 = S_IRUSR | S_IWUSR;
            WriteLine("Result = " + chmod(Path.GetFullPath("secret.txt"), _0600));
        }
    }
  • 相关阅读:
    图形界面 Fedora Core 12/Core 11 How to log in GUI as r
    nis_client.txt
    nis_server.txt
    passwd
    samba.set
    【22.48%】【codeforces 689D】Friends and Subsequences
    【71.76%】【codeforces 732A】Buy a Shovel
    【56.74%】【codeforces 732B】Cormen --- The Best Friend Of a Man
    【43.26%】【codeforces 732C】Sanatorium
    【37.50%】【codeforces 732D】Exams
  • 原文地址:https://www.cnblogs.com/dengquan/p/14923140.html
Copyright © 2011-2022 走看看