Skip to content

Filesystem

iOS, Android, Electron, PWA

The Filsystem API provides a NodeJS-like API for working with files on the device.

Methods

FilesystemBridge.AppendFile()

FilesystemBridge.DeleteFile()

FilesystemBridge.GetUri()

FilesystemBridge.Mkdir()

FilesystemBridge.ReadFile()

FilesystemBridge.ReadDir()

FilesystemBridge.Rmdir()

FilesystemBridge.Stat()

FilesystemBridge.WriteFile()

Example

    private static async void FilesystemWriteFile() {
        try {
            var options = new FileWriteOptions {
                path =  "text.txt",
                data = "File write test",
                directory = FilesystemDirectory.Documents,
                encoding = FilesystemEncoding.UTF8
            };
            var obj = await FilesystemBridge.WriteFile(options);
            Console.WriteLine(obj);
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemReadFile() {
        try {
            var options = new FileReadOptions {
                path =  "text.txt",
                directory = FilesystemDirectory.Documents,
                encoding = FilesystemEncoding.UTF8
            };
            var obj = await FilesystemBridge.ReadFile(options);
            Console.WriteLine($"Data from file: {obj.data}");
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemAppendFile() {
        try {
            var options = new FileAppendOptions {
                path =  "text.txt",
                data = "File Append test",
                directory = FilesystemDirectory.Documents,
                encoding = FilesystemEncoding.UTF8
            };
            var obj = await FilesystemBridge.AppendFile(options);
            Console.WriteLine(obj);
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemDeleteFile() {
        try {
            var options = new FileDeleteOptions {
                path =  "text.txt",
                directory = FilesystemDirectory.Documents,
            };
            var obj = await FilesystemBridge.DeleteFile(options);
            Console.WriteLine(obj);
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemMkdir() {
        try {
            var options = new MkdirOptions {
                path =  "foo/baa/doo",
                directory = FilesystemDirectory.Documents,
                createIntermediateDirectories = true
            };
            var obj = await FilesystemBridge.Mkdir(options);
            Console.WriteLine(obj);
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemStat() {
        try {
            var options = new StatOptions {
                path =  "foo/baa/doo",
                directory = FilesystemDirectory.Documents
            };
            var obj = await FilesystemBridge.Stat(options);
            Console.WriteLine($"{obj.type} : {obj.ctime} : {obj.mtime} : {obj.size} : {obj.uri}");
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemRmdir() {
        try {
            var options = new RmdirOptions {
                path =  "foo/baa/doo",
                directory = FilesystemDirectory.Documents
            };
            var obj = await FilesystemBridge.Rmdir(options);
            Console.WriteLine(obj);
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemReaddir() {
        try {
            var options = new ReaddirOptions {
                path =  "",
                directory = FilesystemDirectory.Documents
            };
            var obj = await FilesystemBridge.ReadDir(options);
            foreach (var file in obj.files) {
                Console.WriteLine(file);
            }
        }
        catch (Exception e) {
            // Handle error
        }
    }

    private static async void FilesystemGetUri() {
        try {
            var options = new GetUriOptions {
                path =  "text.txt",
                directory = FilesystemDirectory.Documents
            };
            var obj = await FilesystemBridge.GetUri(options);
            Console.WriteLine(obj.uri);
        }
        catch (Exception e) {
            // Handle error
        }
    }

API

AppendFile

static Task<FileAppendResult> AppendFile(FileAppendOptions options)

DeleteFile

static Task<FileDeleteResult> DeleteFile(FileDeleteOptions options)

GetUri

static Task<GetUriResult> GetUri(GetUriOptions options) =>

Mkdir

static Task<MkdirResult> Mkdir(MkdirOptions options) =>

ReadFile

static Task<FileReadResult> ReadFile(FileReadOptions options) =>

ReadDir

static Task<ReaddirResult> ReadDir(ReaddirOptions options) =>

Rmdir

static Task<RmdirResult> Rmdir(RmdirOptions options) =>

Stat

static Task<StatResult> Stat(StatOptions options) =>

WriteFile

static Task<FileWriteResult> WriteFile(FileWriteOptions options) =>

Models

FileAppendOptions

    public class FileAppendOptions: FileWriteOptions {}

FileWriteOptions

    public class FileWriteOptions: FileReadOptions {
        public string data; // The data to write
    }

FileReadOptions

    public class FileReadOptions: FileDeleteOptions {
        public FilesystemEncoding encoding; // The encoding to write the file in (defautls to utf8)
    }

GetUriOptions

    public class GetUriOptions: FileDeleteOptions { }

ReaddirOptions

    public class ReaddirOptions: FileDeleteOptions { }

RmdirOptions

    public class RmdirOptions: FileDeleteOptions { }

StatOptions

    public class StatOptions: FileDeleteOptions { }

FileDeleteOptions

    public class FileDeleteOptions {
        public string path; // the filename to delete/read/write
        public FilesystemDirectory directory; // The FilesystemDirectory to store the file in
    }

MkdirOptions

    public class MkdirOptions : FileDeleteOptions {
        public bool createIntermediateDirectories; // Whether to create any missing parent directories as well
    }

GetUriResult

    public class GetUriResult {
        public string uri;
    }

FileAppendResult

    public class FileAppendResult { }

FileDeleteResult

    public class FileDeleteResult { }

MkdirResult

    public class MkdirResult { }

FileWriteResult

    public class FileWriteResult { }

FileReadResult

    public class FileReadResult {
        public string data; // The data read from the file
    }

ReaddirResult

    public class ReaddirResult {
        public string[] files; // Files or directories in directory
    }

RmdirResult

    public class RmdirResult { }

StatResult

    public class StatResult {
        public string type; // type (directory / file)
        public long size; // size of file
        public long ctime; // creation time
        public long mtime; // modified time
        public string uri; // path
    }

FilesystemDirectory

    public enum FilesystemDirectory {
        Application,
        Documents,
        Data,
        Cache,
        External,
        ExternalStorage
    }

FilesystemEncoding

    public enum FilesystemEncoding {
        UTF8,
        ASCII,
        UTF16,
    }

Comments