-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathAppConfigReader.cs
More file actions
47 lines (44 loc) · 1.55 KB
/
AppConfigReader.cs
File metadata and controls
47 lines (44 loc) · 1.55 KB
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
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
namespace TestStack.White.Configuration
{
/// <summary>
/// Configuration reader which reads from the app.config file
/// </summary>
public class AppConfigReader : ConfigurationReaderBase
{
private NameValueCollection nameValues;
public AppConfigReader(string sectionGroup, string sectionName)
{
nameValues = (NameValueCollection)ConfigurationManager.GetSection(sectionGroup + "/" + sectionName);
if (nameValues == null)
{
nameValues = new NameValueCollection();
}
}
protected override void FillConfigurationFromReaderInternal(ConfigurationBase configuration)
{
// Overwrite the values contained in the section
foreach (var key in nameValues.AllKeys)
{
var value = nameValues.Get(key);
if (value != null)
{
configuration.SetValue(key, value);
}
}
// Overwrite the values from the AppSettings
var allKeys = new List<string>(nameValues.AllKeys);
allKeys.AddRange(configuration.DefaultValues.Keys);
foreach (var key in allKeys)
{
var value = ConfigurationManager.AppSettings[key];
if (value != null)
{
configuration.SetValue(key, value);
}
}
}
}
}