Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In monodroid or monotouch what should I use instead of app.config for configuration strings?

I want to store development vs production connection strings and configuration strings in a monodroid project. I would normally store it as app settings in a web.config or an app.config, but how should I do it in monodroid and monotouch projects? I would also like for it to switch configurations automatically between debug and release builds just as visual studio does with *.config files. In an iOS app I could store these in a plist but I'd like a cross platform solution in mono.

How would I do this in monodroid or monotouch?

like image 340
MonkeyBonkey Avatar asked Dec 04 '12 12:12

MonkeyBonkey


People also ask

How to access app settings in c#?

To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below.

What is Configuration Manager in C#?

ConfigurationManager is the class which helps to read data from configurations. Provides access to configuration files for client applications. To use the ConfigurationManager class, your project must reference the System. Configuration assembly.

How read app config file in VB NET?

You can mark the settings as public in the C# project ("Access Modifier" in the property pane of settings) and then you can access it from the vb project (don't forget to add the reference).

Can I use ConfigurationManager in .NET core?

ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.


2 Answers

You should just use a static class with #if declarations.

Something like:

public static class Configuration {
#if DEBUG
    public const string ConnectionString = "debug string";
#else
    public const string ConnectionString = "release string";
#endif
}

The benefit to using app.config is the ability to change these settings on the file system without recompiling. On mobile, there isn't a good way (especially on iOS) to edit the file after it's deployed. So it's generally better to just use a static class and redeploy when you need to change the values. This will also work on all platforms, because it is just C# code doing the work.

like image 106
jonathanpeppers Avatar answered Sep 17 '22 06:09

jonathanpeppers


there's a Xamarin centric AppSetting reader available at https://www.nuget.org/packages/PCLAppConfig it is pretty useful for continuous delivery;

use as per below:

1) Add the nuget package reference to your pcl and platforms projects.

2) Add a app.config file on your PCL project, then as a linked file on all your platform projects. For android, make sure to set the build action to 'AndroidAsset', for UWP set the build action to 'Content'. Add you settings keys/values: <add key="config.text" value="hello from app.settings!" />

3) Initialize the ConfigurationManager.AppSettings on each of your platform project, just after the 'Xamarin.Forms.Forms.Init' statement, that's on AppDelegate in iOS, MainActivity.cs in Android, App in UWP/Windows 8.1/WP 8.1:

ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);

3) Read your settings : ConfigurationManager.AppSettings["config.text"];

like image 32
Ben Ishiyama-Levy Avatar answered Sep 17 '22 06:09

Ben Ishiyama-Levy