Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

README.md

DewXamarinFiles

A library for xamarin forms to manage files and local settings.

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

using DewCore.Xamarin.Files;
using DewCore.Extensions.Strings;
public class Example
{
    public async Task CreateFileWithText(string toWrite)
    {
        var basePath = DewXamarinFiles.ApplicationPath();
        if(!DewXamarinFiles.CheckFileExists(basePath+Path.DirectorySeparator+"textfile.txt"))
            await DewXamarinFiles.WriteLocalFileTextAsync(towrite,"textfile.txt",basePath);
        if(!DewXamarinFiles.CheckFileExists(basePath+Path.DirectorySeparator+"bytefile"))
            await DewXamarinFiles.WriteLocalFileTextAsync(towrite.ToBytes(),"bytefile",basePath);
        string text = await DewXamarinFiles.ReadLocalFileTextAsync(basePath+Path.DirectorySeparator+"textfile.txt");
        byte[] bytes = await DewXamarinFiles.ReadLocalFileAsync(basePath+Path.DirectorySeparator+"textfile.txt");
    }
}

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

using DewCore.Xamarin.Files;
using DewCore.Extensions.Strings;
public class Example
{
    public class User
    {
        public string Name {get;set;}
        public string Surname {get;set;}
    }
//-----------------------------------------------------------------------
    public async Task<bool> WriteToken()
    {
        if(!(await DewXamarinFiles.CheckLocalSettingExists("token")))
            return false;
        var token = await DewXamarinFiles.ReadLocalSettings("token");
        if(token.Length < 10)
            while(token.Length < 10)
                token += "0";
        await DewXamarinFiles.WriteLocalSetting("token",token);
        return true;
    }
//-----------------------------------------------------------------------
    public async Task WriteUser(User u)
    {
        await DewXamarinFiles.WriteLocalSetting<User>("user",u);    
    }
//-----------------------------------------------------------------------
    public async Task<User> ReadUser()
    {
        if(!(await DewXamarinFiles.CheckLocalSettingExists("user")))
            return null;
        return DewXamarinFiles.ReadLocalSetting<User>("user");
    }
//-----------------------------------------------------------------------
}

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.

About

Andrea Vincenzo Abbondanza

Donate

Help me to grow up, if you want

About

A library for xamarin forms to manage files and local settings

Topics

Resources

License

Releases

No releases published

Packages

No packages published

Languages