File permissions on Linux/Unix with .NET Core

后端 未结 4 1241
刺人心
刺人心 2020-12-05 13:25

I am trying to learn how to set file permissions on Linux/Unix with .NET Core. I already found a question on here that points me in the direction of System.IO.FileSystem, b

4条回答
  •  没有蜡笔的小新
    2020-12-05 14:05

    I solved this problem by just starting a new process and executing bash chmod commands.

    Example:

    public static void Exec(string cmd)
    {
        var escapedArgs = cmd.Replace("\"", "\\\"");
            
        using var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
                FileName = "/bin/bash",
                Arguments = $"-c \"{escapedArgs}\""
            }
        };
    
        process.Start();
        process.WaitForExit();
    }
    

    and then:

    Exec("chmod 644 /path/to/file.txt");
    

    You can also use this Exec method to run any other type of bash commands.

提交回复
热议问题