Search code examples
configurationxamarin.androidxamarin.mobile

Monodroid: Where should I put configuration settings?


From Miguel de Icaza:

We use a library profile that is better suited for mobile devices, so we removed features that are not necessary (like the entire System.Configuration stack, just like Silverlight does).

After years of .NET development, I'm accustomed to storing configuration settings in web.config and app.config files.

  • When using Mono for Android, where should I put my configuration settings?
    • If it matters, I'd like to store different configuration settings for different build configurations as well.

Solution

  • I would probably recommend using shared preferences and compilation symbols to manage different configurations. Below is an example of how you can use a preferences file to add or change keys based on the compilation symbols. Additionally, you could create a separate preferences file that is only available for a particular configuration. Because these keys are not available on all configurations, make sure to always perform checks for them before using.

    var prefs = this.GetSharedPreferences("Config File Name", FileCreationMode.Private);
    var editor = prefs.Edit();
    
    #if MonoRelease
    editor.PutString("MyKey", "My Release Value");
    editor.PutString("ReleaseKey", "My Release Value");
    #else
    editor.PutString("MyKey", "My Debug Value");
    editor.PutString("DebugKey", "My Debug Value");
    #endif
    
    editor.PutString("CommonKey", "Common Value");
    editor.Commit();