In This Topic
The NFile class contains properties and methods for working with files in a platform independent way. In most cases you will get instances of the NFile class using Open File and Save File dialogs (see Common Dialogs for more info), or using the GetFile or GetFiles methods of an NFolder instance. When necessary you can also create a NFile instance for a file system path by using the GetFile method of the NFileSystem class.
Since some platforms can't read or write files synchronously, all of the file manipulation methods are asynchronous and work with promises.
The methods of the NFile class are organized into the following major categories:
Files Reading and Writing - Streams
The following methods of the NFile class let you create, read and write files:
Method |
Description |
NPromise<Stream> CreateAsync() |
Creates a file for both reading and writing. If the file already exists it is overwritten. |
NPromise<Stream> OpenAsync() |
Opens an existing file or creates a new file for both reading and writing. |
NPromise<Stream> OpenReadAsync() |
Opens the file for reading. |
NPromise<Stream> OpenWriteAsync() |
Opens an existing file or creates a new file for writing. |
For example, to read the content of a file as an UTF-8 encoded string, you can use the following piece of code:
Read File |
Copy Code
|
NFile file = NFileSystem.Current.GetFile(@"C:\Documents\MyFile.txt");
file.OpenRead().Then(
delegate (Stream stream)
{
using (stream)
{
byte[] fileData = NStreamHelpers.ReadToEnd(stream);
string fileContents = NEncoding.UTF8.GetString(fileData);
}
},
delegate (Exception ex)
{
// Reading of the file failed
}
);
|
Files Reading and Writing - Bytes and Text
NFile provides the following helper method for reading and writing bytes and text:
Method |
Description |
NPromise<byte[]> ReadAllBytesAsync() |
Opens the file for reading, reads it and returns its byte content. |
NPromise<string> ReadAllTextAsync() |
Opens the file for reading, and reads the contents of this file as a string using UTF-8 text encoding. |
NPromise<string> ReadAllTextAsync(NEncoding encoding) |
Opens the file for reading, and reads the contents of this file as a string using the specified text encoding. |
NPromise<NUndefined> WriteAllBytesAsync(byte[] bytes) |
Writes the given byte array to the file. If the file exists, it is overwritten. |
NPromise<NUndefined> WriteAllTextAsync(string text) |
Writes the given text to the file using the UTF-8 encoding. No byte order mark (BOM) is written. |
NPromise<NUndefined> WriteAllTextAsync(string text, NEncoding encoding, bool includeBom) |
Writes the given text to the file using the specified encoding. Optionally writes a byte order mark (BOM). |
For example to read a file as UTF-8 encoded string, you can use the following piece of code:
Read File |
Copy Code
|
NFile file = NFileSystem.Current.GetFile(@"C:\Documents\MyFile.txt");
file.ReadAllBytes().Then(
delegate (byte[] fileData)
{
string fileContents = NEncoding.UTF8.GetString(fileData);
},
delegate (Exception ex)
{
// Reading of the file failed
}
);
|
Files Manipulation and Information
The following methods of the NFile class help you manipulate files, as well as obtain information about them.
Method |
Description |
File Specific Operations
|
NPromise<long> GetLengthAsync() |
Gets the size of the file in bytes. |
NPromise<NFile> CopyAsync(string destinationPath, bool overrideDestination) |
Copies the file to the specified destination folder. Optionally overrides the file with the same name, if it exists in the specified destination folder. |
NPromise<NUndefined> MoveAsync(string destinationPath, bool overrideDestination) |
Moves the file to the specified destination folder. Optionally overrides the file with the same name, if it exists in the specified destination folder. |
Inherited from NFileSystemObject
|
NPromise<NUndefined> DeleteAsync() |
Deletes the file |
NPromise<bool> ExistsAsync() |
Checks whether the file exists. |
NFolder GetParentFolder() |
Get the folder in which the file resides. |
NPromise<ENFileSystemObjectAttributes> GetAttributesAsync() |
Gets the attributes of this file or directory. |
NPromise<NUndefined> SetAttributesAsync(ENFileSystemObjectAttributes attributes) |
Sets the attributes of this file or directory. |
NPromise<DateTime> GetTimeAsync(ENFileSystemObjectTime time) |
Gets the specifies file system object time stamp. |
NPromise<NUndefined> SetTimeAsync(ENFileSystemObjectTime time, DateTime value) |
Sets the specifies file system object time stamp. |
The NFile class also provides the following properties:
Property |
Description |
File Specific Properties
|
NameWithoutExtension |
Gets the name of this file without the extension. |
Extension |
Gets the extension of this file without the dot, for example: "txt", "rtf", "docx", etc. If the given file name does not have an extension, String.Empty is returned. |
Inherited from NFileSystemObject
|
Uri |
Gets the file URI of this file system object. |
Path |
Gets the full path of this file system object in the current platform notation (Windows or Unix). |
Name |
Returns only the name of the file system object, without the path information.
For example if the path is "C:\Documents\MyFile.txt", this method returns "MyFile.txt".
To get the full path to this file system object, use the Path property. |
FileSystem |
Gets the file system associated with this file system object. |
See Also