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, but I can't seem to find any documentation on how to use it.
In a nutshell, I'd like to chmod a file 644 from a .net core application that only runs on Linux, but am at a loss on how to proceed.
I solved this problem by just starting a new process and executing bash
chmod
commands.Example:
and then:
You can also use this
Exec
method to run any other type of bash commands.At the moment, there is no built in API in .NET Core for this. However, the .NET Core team is working to make
Mono.Posix
available on .NET Core. This exposes API to do this kind of operation in managed code. See https://github.com/dotnet/corefx/issues/15289 and https://github.com/dotnet/corefx/issues/3186. You can try an early version of this API here: https://www.nuget.org/packages/Mono.Posix.NETStandard/1.0.0-beta1If you don't want to use Mono.Posix, you can implement this same functionality by invoking native code. Using P/Invoke, you can call the
chmod
function fromlibc
. Seeman 2 chmod
for more details on the native API.