Skip to content

Commit

Permalink
Added simple example project
Browse files Browse the repository at this point in the history
  • Loading branch information
tparvi committed Aug 4, 2011
1 parent 96bd8a8 commit 29ce7cf
Show file tree
Hide file tree
Showing 7 changed files with 1,154 additions and 0 deletions.
30 changes: 30 additions & 0 deletions AppSettings.sln
Expand Up @@ -5,20 +5,50 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApplicationSettings", "Appl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApplicationSettingsTests", "UnitTests\ApplicationSettingsTests\ApplicationSettingsTests.csproj", "{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleExample", "Examples\SimpleExample\SimpleExample.csproj", "{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{067F5996-6800-43AE-B48D-25A729E7950D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Debug|x86.ActiveCfg = Debug|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Release|Any CPU.Build.0 = Release|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{067F5996-6800-43AE-B48D-25A729E7950D}.Release|x86.ActiveCfg = Release|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Debug|x86.ActiveCfg = Debug|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Release|Any CPU.Build.0 = Release|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5C684BA2-B7E9-4AF6-8B00-3C2EBD69CB41}.Release|x86.ActiveCfg = Release|Any CPU
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Debug|Mixed Platforms.Build.0 = Debug|x86
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Debug|x86.ActiveCfg = Debug|x86
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Debug|x86.Build.0 = Debug|x86
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Release|Any CPU.Build.0 = Release|Any CPU
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Release|Mixed Platforms.ActiveCfg = Release|x86
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Release|Mixed Platforms.Build.0 = Release|x86
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Release|x86.ActiveCfg = Release|x86
{514D20D7-8AE6-4F14-A918-ECD9A8942BBE}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
21 changes: 21 additions & 0 deletions ApplicationSettings/AppSettings.cs
Expand Up @@ -21,6 +21,14 @@ public AppSettings(string fileName)
this.Configuration = OpenConfigurationFile(fileName);
}

/// <summary>
/// Initializes a new instance of the <see cref="AppSettings"/> class.
/// </summary>
public AppSettings()
{
this.Configuration = OpenConfigurationFile();
}

/// <summary>
/// Gets absolute path to the configuration file.
/// </summary>
Expand Down Expand Up @@ -298,6 +306,19 @@ protected static Configuration OpenConfigurationFile(string fileName)
return ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
}

/// <summary>
/// Opens the configuration file for the executable.
/// </summary>
/// <returns>
/// Configuration object.
/// </returns>
protected static Configuration OpenConfigurationFile()
{
var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ValidateFilePath(configuration.FilePath);
return configuration;
}

/// <summary>
/// Gets the connection string from configuration.
/// </summary>
Expand Down
136 changes: 136 additions & 0 deletions Examples/SimpleExample/Program.cs
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleExample
{
using System.Globalization;

using ApplicationSettings;

public enum ProjectStatus
{
Unknown,
Started,
Finished
}

public class Program
{
public static AppSettings settings;

public static void Main(string[] args)
{
try
{
// Open the default app.config file which in this case is SimpleExample.exe.config
// and it is located in the same folder as the SimpleExample.exe
settings = new AppSettings();

ReadStringValues();
ReadStandardDataTypes();
ReadEnumerations();
ReadNullableValues();
ReadUsingCustomFormatProvider();
ReadOptionalValues();
ReadUsingCustomConversionFunction();
ReadConnectionString();
}
catch (Exception exp)
{
Console.Error.WriteLine(exp.Message);
}
}

public static void ReadStringValues()
{
// String values can be read without having to specify the type. This avoids the
// unnecessary type conversion.
var firstStringValue = settings.GetValue("FirstStringValue");
Console.WriteLine("FirstStringValue is: {0}", firstStringValue);

// Of course nothing forbids you of using the type when reading string values
var secondStringValue = settings.GetValue<string>("SecondStringValue");
Console.WriteLine("SecondStringValue is: {0}", secondStringValue);
}

public static void ReadStandardDataTypes()
{
// Standard .NET data types can be read type safely
var intValue = settings.GetValue<int>("IntValue");
Console.WriteLine("IntValue is {0}", intValue);

var doubleValue = settings.GetValue<double>("DoubleValue");
Console.WriteLine("DoubleValue is {0}", doubleValue);
}

public static void ReadEnumerations()
{
// You can read enumerations which are stored as strings
var projectStatus = settings.GetValue<ProjectStatus>("EnumString");
Console.WriteLine("Enum saved as string has value: {0}", projectStatus);

// You can also read enumerations which are stored as integers
projectStatus = settings.GetValue<ProjectStatus>("EnumNumeric");
Console.WriteLine("Enum saved as integer has value: {0}", projectStatus);

}

public static void ReadNullableValues()
{
// 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);

// If the value is not empty you can access it using the .Value property
var intValue = settings.GetValue<int?>("IntValue");
Console.WriteLine("IntValue is {0}", intValue.Value);
}

public static void ReadUsingCustomFormatProvider()
{
// Sometimes you might have values which have been saved using
// different culture. E.g. double value might have been saved
// using comma as decimal separator. In those cases you can
// use custom format provider
var doubleValue = settings.GetValue<double>("DoubleWithFinnishLocale", CultureInfo.GetCultureInfo("fi-FI"));
Console.WriteLine("Double with finnish locale is: {0}", doubleValue);
}

public static void ReadOptionalValues()
{
// If the setting is optional then you can specify
// default value which is returned in case setting does not exist
var value = settings.GetOptionalValue("NonExistingSetting", "DefaultValue");
Console.WriteLine("Value for NonExistingSetting is: {0}", value);

// Of course you can use default values type safely
var intValue = settings.GetOptionalValue<int>("NonExistingIntValue", 123);
Console.WriteLine("Value for NonExistingIntValue is: {0}", intValue);
}

public static void ReadUsingCustomConversionFunction()
{
// Sometimes you want to specify custom converson function for your value
var value = settings.GetValue<int>("SecondIntValue", (setting, rawValue) => int.Parse(rawValue) * 10);

Console.WriteLine("SecondIntValue using custom conversion function is: {0}", value);
}

public static void ReadConnectionString()
{
// If you have only single connection string you can access it by
// using the ConnectionString property. Unfortunately .NET configuration
// files inherit connection string from upper level configurations and
// your machine.config is bound to have some. Fortunately you can add
// the <clear/> tag to get rid of them. See the app.config file for example
var cs = settings.ConnectionString;
Console.WriteLine("ConnectionString is: {0}", cs);

// Of course you can still access connection string by their name
cs = settings.GetConnectionString("MyDatabase");
}
}
}
36 changes: 36 additions & 0 deletions Examples/SimpleExample/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SimpleExample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("SimpleExample")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3d8f2538-1041-42dc-a41b-029fd0519430")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit 29ce7cf

Please sign in to comment.