Cross platform config file / user preferences storage

AndyFlisherAndyFlisher GBBeta, University ✭✭✭

Apologies if this has been answered but my search foo is failing me. I'm building a multi platform app that will be released under iOS, Android and Windows Phone, the core network service code will be reused on mac and Windows desktop too. Nature of the app is that I need to store some per user config! preferences (username, password, hostname etc) and am looking for the most cross platform config file format and storage methods.

Normally I use nini to read / write ini file formats on the desktop, but don't think that will work on mobile, is there a better universal format, XML, plain text, or should I look at sqllite with a simple table?, seems over kill?

What are others doing?

Tia

Posts

  • SKallSKall USMember ✭✭✭✭
    edited December 2013

    Nini works on mobile but I would not use it as it is more convenient to create a settings DTO and serialize/deserialize it to XML/JSON etc.

    I have used abstracted serializer in the below example from my own library but you can choose to use Json.NET, ServiceStack or the default BCL serializers instead.

    using System.IO;
    using SimplyMobile.Text;
    
    namespace SettingsSample
    {
        public class Settings
        {
            public string UserName { get; set; }
    
            public string SomeOtherProperty { get; set; }
    
            public static Settings Load(string settingsFile, ITextSerializer serializer)
            {
                using (var streamReader = new StreamReader (settingsFile)) 
                {
                    return serializer.DeserializeFromReader<Settings> (streamReader);
                }
            }
    
            public void Save(string settingsFile, ITextSerializer serializer)
            {
                using (var textWriter = new StreamWriter (settingsFile)) 
                {
                    serializer.SerializeToWriter (this, textWriter);
                }
            }
        }
    }
    
  • AndyFlisherAndyFlisher GBBeta, University ✭✭✭

    Hmm, when you see it in code it is relatively simple, and already using Json.net so no need for another lib, not sure why inn was giving me grief.

    Guess the platform specific file storage differences can't be easily remedied, but probably worthy of a class library as I'll reuse - strikes me as something worthy of a component though

  • SKallSKall USMember ✭✭✭✭

    I would have to check what is available on WP8 but on iOS and Android you should be able to use IsolatedStorage or Environment. That's why I put the filename in the function just to be able to use different path per platform.

                var settingsFile = Path.Combine (
                    Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
                    "settings.json");
    
Sign In or Register to comment.