Skip to content

Simple usage examples

tparvi edited this page Sep 4, 2011 · 1 revision

The source code contains many ready to run examples including basic use cases, extensibility, dependency injection and reading web.config. Below are some of those examples

###Creating AppSettings // Creating for the entry point of your program. Entry // point can be foo.exe (foo.exe.config) or foo.dll (foo.dll.config) var entryPoint = AppSettings.CreateForAssembly(Assembly.GetEntryAssembly(), FileOption.None);

        // If you call CreateForCallingAssembly from foo.dll then
        // the name of the configuration file should be foo.dll.config
        AppSettings.CreateForCallingAssembly(FileOption.FileMustExist);

        // If your settings are stored under AppData folder you can create
        // AppSettings for the current user.
        var currentUserOfApp = AppSettings.CreateForCurrentUser(
              Assembly.GetEntryAssembly(), "MyApplication", FileOption.None);

###Reading string value // String values can be read without having to specify the type. This avoids the // unnecessary type conversion. var firstStringValue = settings.GetValue("FirstStringValue");

###Reading standard data types type safely var intValue = settings.GetValue("IntValue");

###Reading optional values // If the setting OptionalValue does not exist then 123 is returned var intValue = settings.GetOptionalValue("OptionalValue", 123);

###Reading enumerations // You can read enumerations which are stored as strings var projectStatus = settings.GetValue("EnumString");

        // You can also read enumerations which are stored as integers
        projectStatus = settings.GetValue<ProjectStatus>("EnumNumeric");

###Reading nullable values // Nullable values are also supported. When you are reading Nullable value // empty string in the app.config is considered as a null value var nullableIntValue = settings.GetValue<int?>("EmptyIntValue"); Console.WriteLine("EmptyIntValue HasValue returns: {0}", nullableIntValue.HasValue);

###Using custom conversion functions // Sometimes you want to specify custom converson function for your value var value = settings.GetValue("SecondIntValue", (setting, rawValue) => int.Parse(rawValue) * 10);

###Using custom format provider var doubleValue = settings.GetValue("DoubleWithFinnishLocale", CultureInfo.GetCultureInfo("fi-FI"));

###Reading connection strings cs = settings.GetConnectionString("MyDatabase");

###Reading custom configuration file // If your .config fle is named something else you can // give relative or absolute path to that file var custom = new AppSettings(@"Custom.config"); ###Updating values // Values can be updated using SetValue/SetConnectionString methods // and custom conversion functions and IFormatProvider are supported settings.SetValue("StringValue", "new value"); settings.SetValue("DoubleValue", 1.2d); settings.SetConnectionString("MyDatabase", "Data Source=localhost;Initial Catalog=MyDb;User Id=username;Password=newpassword;"); ###Saving settings settings.Save();

Clone this wiki locally