-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.cs
50 lines (44 loc) · 1.64 KB
/
Settings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//-----------------------------------------------------------------------
// <copyright file="Settings.cs" company="CodeRanger.com">
// Copyright (c) CodeRanger.com. All rights reserved.
// </copyright>
// <author>Dan Petitt</author>
// <comment />
//-----------------------------------------------------------------------
namespace WebTest
{
using System;
using System.Configuration;
public class Settings : ISettings
{
public Settings()
{
try
{
var appSettings = ConfigurationManager.AppSettings;
if( appSettings.Count == 0 )
{
Console.WriteLine( "ERROR: AppSettings is empty." );
throw new ArgumentException( "Config file did not have any settings" );
}
ThreadCount = GetAppSettingValueAsInt( "ThreadCount", 1 );
}
catch( ConfigurationErrorsException ex )
{
Console.WriteLine( "Error reading settings" );
throw new ArgumentException( "Error reading settings because: " + ex.Message );
}
}
public int ThreadCount { get; private set; }
private string GetAppSettingValueAsString( string name, string defaultValue = "" )
{
var v = ConfigurationManager.AppSettings[ name ];
return v == null ? defaultValue : v;
}
private int GetAppSettingValueAsInt( string name, int defaultValue = 0 )
{
var v = ConfigurationManager.AppSettings[ name ];
return v == null ? defaultValue : Convert.ToInt32( v );
}
}
}