Q:

Test file permissions on Mono

I'm working on crossplatform app with Mono. I want to check permissions for RW-access to user that runs application. On NT I can use .GetAccessControl methods (on Mono throws PlatformNotSupported exception), but what to do with *nix and MacOS? Is there any crossplatfrom solutions?

A:

Mono.Unix has UNIX specific implementations:
var ufi = new UnixFileInfo("/tmp/test.cpp");
ufi.CanAccess(AccessModes.F_OK); // is a file/directory
ufi.CanAccess(AccessModes.R_OK); // accessible for reading
ufi.CanAccess(AccessModes.W_OK); // accessible for writing
ufi.CanAccess(AccessModes.X_OK); // accessible for executing
FileSpecialAttributes sa = ufi.FileSpecialAttributes; //setuid, setgid and sticky bits
FileAccessPermissions fa = ufi.FileAccessPermissions;

FileAccessPermissions is defined as:
[Flags ()]
public enum FileAccessPermissions {
    UserReadWriteExecute,
    UserRead,
    UserWrite,
    UserExecute,
    GroupReadWriteExecute,
    GroupRead,
    GroupWrite,
    GroupExecute,
    OtherReadWriteExecute,
    OtherRead,
    OtherWrite,
    OtherExecute,
    DefaultPermissions,
    AllPermissions
}

