Class UnixUtils
Provides helper methods for Unix-specific features of the Mono library.
public static class UnixUtils
- Inheritance
-
objectUnixUtils
Remarks
Make sure to check IsUnix before calling any methods in this class to avoid exceptions.
Properties
CpuType
The CPU type as reported by the "uname" system call (after applying some normalization).
public static string CpuType { get; }
Property Value
- string
HasGui
true
if there is an X Server running or the current operating system is MacOS X.
public static bool HasGui { get; }
Property Value
- bool
IsFreeBSD
true
if the current operating system is FreeBSD.
public static bool IsFreeBSD { get; }
Property Value
- bool
IsLinux
true
if the current operating system is Linux.
public static bool IsLinux { get; }
Property Value
- bool
IsMacOSX
true
if the current operating system is MacOS X.
public static bool IsMacOSX { get; }
Property Value
- bool
IsUnix
true
if the current operating system is a Unixoid system (e.g. Linux or MacOS X).
public static bool IsUnix { get; }
Property Value
- bool
OSName
The operating system name as reported by the "uname" system call.
public static string OSName { get; }
Property Value
- string
Methods
CreateHardlink(string, string)
Creates a new Unix hard link between two files.
public static void CreateHardlink(string sourcePath, string targetPath)
Parameters
sourcePath
stringThe path of the link to create.
targetPath
stringThe absolute path of the existing file to point to.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
CreateSymlink(string, string)
Creates a new Unix symbolic link to a file or directory.
public static void CreateSymlink(string sourcePath, string targetPath)
Parameters
sourcePath
stringThe path of the link to create.
targetPath
stringThe path of the existing file or directory to point to (relative to
sourcePath
).
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
GetFileSystem(string)
Determines the file system type a file or directory is stored on.
public static string? GetFileSystem(string path)
Parameters
path
stringThe path of the file.
Returns
- string
The name of the file system in fstab format (e.g. ext3 or ntfs-3g);
null
if unable to determine.
Remarks
Only works on Linux, not on other Unixes (e.g. MacOS X).
Exceptions
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
GetInode(string)
Returns the Inode ID of a file.
public static long GetInode(string path)
Parameters
path
stringThe path of the file.
Returns
- long
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
GetXattr(string, string)
Gets an extended file attribute.
public static byte[]? GetXattr(string path, string name)
Parameters
path
stringThe path of the file to read the attribute from.
name
stringThe name of the attribute to read.
Returns
- byte[]
The contents of the attribute as a byte array;
null
if there was a problem reading the file.
IsExecutable(string)
Checks whether a file is marked as Unix-executable.
public static bool IsExecutable(string path)
Parameters
path
stringThe file to check for executable rights.
Returns
- bool
true
ifpath
points to an executable;false
otherwise.
Remarks
Will return false
for non-existing files.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
IsRegularFile(string)
Checks whether a file is a regular file (i.e. not a device file, symbolic link, etc.).
public static bool IsRegularFile(string path)
Parameters
path
string
Returns
- bool
true
ifpath
points to a regular file;false
otherwise.
Remarks
Will return false
for non-existing files.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
IsSymlink(string)
Checks whether a file is a Unix symbolic link.
public static bool IsSymlink(string path)
Parameters
path
stringThe path of the file to check.
Returns
- bool
true
ifpath
points to a symbolic link;false
otherwise.
Remarks
Will return false
for non-existing files.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
IsSymlink(string, out string)
Checks whether a file is a Unix symbolic link.
public static bool IsSymlink(string path, out string target)
Parameters
path
stringThe path of the file to check.
target
stringReturns the target the symbolic link points to if it exists.
Returns
- bool
true
ifpath
points to a symbolic link;false
otherwise.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
MakeReadOnly(string)
Removes write permissions for everyone on a filesystem object (file or directory).
public static void MakeReadOnly(string path)
Parameters
path
stringThe filesystem object (file or directory) to make read-only.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
MakeWritable(string)
Sets write permissions for the owner on a filesystem object (file or directory).
public static void MakeWritable(string path)
Parameters
path
stringThe filesystem object (file or directory) to make writable by the owner.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
Rename(string, string)
Renames a file. Atomically replaces the destination if present.
public static void Rename(string source, string destination)
Parameters
source
stringThe path of the file to rename.
destination
stringThe new path of the file. Must reside on the same file system as
source
.
Exceptions
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
SetExecutable(string, bool)
Marks a file as Unix-executable or not Unix-executable.
public static void SetExecutable(string path, bool executable)
Parameters
path
stringThe file to mark as executable or not executable.
executable
booltrue
to mark the file as executable,true
to mark it as not executable.
Exceptions
- InvalidOperationException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).
SetXattr(string, string, byte[])
Sets an extended file attribute.
public static void SetXattr(string path, string name, byte[] data)
Parameters
path
stringThe path of the file to set the attribute for.
name
stringThe name of the attribute to set.
data
byte[]The data to write to the attribute.
Exceptions
- IOException
The underlying Unix subsystem failed to process the request (e.g. because of insufficient rights).