Xamarin Forms files library

We’ve got a lot of projects in xamarin in these days: Forms, IOS, Android, etc.

My last work (in progress) is about a Xamarin Froms app and, in this app, I need to work hard with files.

So I’ve created a little library to manage files and local settings in .net standard. Works very well for UWP, Android, IOS for now.

You can find it on GitHub

How to use

This is a static class, so you can use the methods and attributes directly.

Files

Here the methods/attributes that help you to manage files.

Attributes

  • BufferSize: int – Is the buffer size for read/write files

Methods

  • ReadLocalFileAsync: (path: string : The path to the file) : return byte[]
  • ReadLocalFileTextAsync: (path: string : The path to the file) : return string
  • WriteLocalFileAsync: (s: byte[] : The data to write, name: string : filename, path: string : the path to the folder)
  • WriteLocalFileAsync: (s: string : The data to write, name: string : filename, path: string : the path to the folder)
  • ApplicationPath: return string : the path to the local app folder with write permissions_
  • CheckFileExists: (file: string : file path) : return bool
  • CheckDirectoryExists: (path: string : directory path) return bool

Example

NOTE

In app environemnt, you need to pass always the basePath because you don’t have the permissions to write wherever you want. The methods supports absolute paths just for the future, in the eventually of MacOS, Linux, PC xamarin forms supports.

Local Settings

Here the methods/attributes that help you to manage local settings.

Attributes

  • SettingsName: string – Is the local settings file name (without extension), default value is “__dew_loc_set

Methods

  • WriteLocalSettings: (string: key : local setting key, string: option : local setting value)
  • WriteLocalSettings<T>: (string: key : local setting’s key, T: option : local setting value) – T must implmenent a ToString method to serialize
  • CheckLocalSettingExists: (string: key : local setting’s key) : return bool
  • ReadLocalSetting: (string: key : local setting’s key): return string
  • ReadLocalSetting<T>: (string: key : local setting’s key): return T
  • SerializeObject<T>: (T toSerialize : Object to serialize): return string
  • DeserializeObject<T>: (string toDeserialize : Object to deserialize): return T

Example

Note

  • Type T must be serializable
  • When you try to read a local setting and the file doesn’t exist, it will throw a FileNotFoundException, so Use CheckLocalSettingExists before
  • When you call CheckLocalSettings, if the file doesn’t exist it will be created with “{}” object into.
  • When you call a Write function, if the file doesn’t exist it will be created with “{}” object into.

Donate

Help me to grow up, if you want