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
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.