File
Struct · namespace System
struct FileWhole files: reading, writing, copying, moving and deleting them (static functions). The paths are passed to the C
library as they are; text is UTF-8 unless an Encoding is given.
var text = try File.ReadAllText("config.txt");try File.WriteAllText("out.txt", text + "\n");Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Exists |
Whether a file (or directory) exists at path. |
Delete |
Deletes the file at path. |
ReadAllBytes |
The contents of a file as bytes. |
ReadAllText |
The contents of a UTF-8 text file. |
WriteAllBytes |
Creates the file or replaces its contents with bytes. |
Copy |
Copies a file; an existing target is replaced only with overwrite. |
Move |
Moves (renames) a file; an existing target is replaced only with overwrite. |
GetLastWriteTime |
When the file was last written, in local time. |
GetLastWriteTimeUtc |
When the file was last written, in UTC. |
SetLastWriteTime |
Sets when the file (or folder) was last written; a local time is converted to UTC. |
SetLastWriteTimeUtc |
Sets when the file (or folder) was last written, in UTC (a local time is converted). |
WriteAllText |
Creates the file or replaces its contents with text (UTF-8). |
Exists
Section titled “Exists”static bool Exists(StringSlice path)Whether a file (or directory) exists at path.
Delete
Section titled “Delete”static IoError<void> Delete(StringSlice path)Deletes the file at path.
Errors
IoError.CannotDelete: the file does not exist or cannot be deleted.
ReadAllBytes
Section titled “ReadAllBytes”static IoError<uint8[]> ReadAllBytes(StringSlice path)The contents of a file as bytes.
Errors
IoError.CannotOpen: the file does not exist or cannot be read.
ReadAllText
Section titled “ReadAllText”static IoError<string> ReadAllText(StringSlice path)The contents of a UTF-8 text file. A leading byte order mark is removed.
Errors
IoError.CannotOpen: the file does not exist or cannot be read.IoError.InvalidText: the file is not valid UTF-8.
static IoError<string> ReadAllText(StringSlice path, Encoding encoding)The contents of a text file in encoding. A leading UTF-8 byte order mark is removed.
Errors
IoError.CannotOpen: the file does not exist or cannot be read.IoError.InvalidText: the file is not valid in the encoding.
WriteAllBytes
Section titled “WriteAllBytes”static IoError<void> WriteAllBytes(StringSlice path, ReadOnlySlice<uint8> bytes)Creates the file or replaces its contents with bytes.
Errors
IoError.CannotCreate: the file cannot be created (a missing folder, no permission).IoError.CannotWrite: writing failed (the disk is full, …).
static IoError<void> Copy(StringSlice source, StringSlice target, bool overwrite)Copies a file; an existing target is replaced only with overwrite.
Errors
IoError.AlreadyExists:targetexists andoverwriteisfalse.IoError.CannotOpen:sourcecannot be read.IoError.CannotCreate:targetcannot be created.
Since 0.04
static IoError<void> Copy(StringSlice source, StringSlice target)Copies a file; an existing target is not replaced.
Errors
IoError.AlreadyExists:targetexists.IoError.CannotOpen:sourcecannot be read.IoError.CannotCreate:targetcannot be created.
Since 0.04
static IoError<void> Move(StringSlice source, StringSlice target, bool overwrite)Moves (renames) a file; an existing target is replaced only with overwrite.
Errors
IoError.CannotOpen:sourcedoes not exist.IoError.AlreadyExists:targetexists andoverwriteisfalse.IoError.CannotMove: the system refused (another drive on AmigaOS, no permission).
Since 0.19
static IoError<void> Move(StringSlice source, StringSlice target)Moves (renames) a file; an existing target is not replaced.
Errors
IoError.CannotOpen:sourcedoes not exist.IoError.AlreadyExists:targetexists.IoError.CannotMove: the system refused (another drive on AmigaOS, no permission).
Since 0.19
GetLastWriteTime
Section titled “GetLastWriteTime”static IoError<DateTime> GetLastWriteTime(StringSlice path)When the file was last written, in local time.
Errors
IoError.CannotOpen: the file does not exist.
Since 0.19
GetLastWriteTimeUtc
Section titled “GetLastWriteTimeUtc”static IoError<DateTime> GetLastWriteTimeUtc(StringSlice path)When the file was last written, in UTC.
Errors
IoError.CannotOpen: the file does not exist.
Since 0.19
SetLastWriteTime
Section titled “SetLastWriteTime”static IoError<void> SetLastWriteTime(StringSlice path, DateTime time)Sets when the file (or folder) was last written; a local time is converted to UTC. On POSIX systems the time
of the last access is set too.
Errors
IoError.CannotWrite: the file does not exist or its time cannot be changed.
Since 0.29
SetLastWriteTimeUtc
Section titled “SetLastWriteTimeUtc”static IoError<void> SetLastWriteTimeUtc(StringSlice path, DateTime time)Sets when the file (or folder) was last written, in UTC (a local time is converted).
Errors
IoError.CannotWrite: the file does not exist or its time cannot be changed.
Since 0.29
WriteAllText
Section titled “WriteAllText”static IoError<void> WriteAllText(StringSlice path, StringSlice text)Creates the file or replaces its contents with text (UTF-8).
Errors
IoError.CannotCreate: the file cannot be created (a missing folder, no permission).IoError.CannotWrite: writing failed (the disk is full, …).
static IoError<void> WriteAllText(StringSlice path, StringSlice text, Encoding encoding)Creates the file or replaces its contents with text in encoding.
Errors
IoError.CannotCreate: the file cannot be created (a missing folder, no permission).IoError.CannotWrite: writing failed (the disk is full, …).