Configuration
using System.Collections;
using System.Configuration.Internal;
using System.IO;
using System.Runtime.Versioning;
namespace System.Configuration
{
public sealed class Configuration
{
private readonly MgmtConfigurationRecord _configRecord;
private readonly object[] _hostInitConfigurationParams;
private readonly Type _typeConfigHost;
private Func<string, string> _assemblyStringTransformer;
private ContextInformation _evalContext;
private ConfigurationLocationCollection _locations;
private ConfigurationSectionGroup _rootSectionGroup;
private Stack _sectionsStack;
private Func<string, string> _typeStringTransformer;
public AppSettingsSection AppSettings => (AppSettingsSection)GetSection("appSettings");
public ConnectionStringsSection ConnectionStrings => (ConnectionStringsSection)GetSection("connectionStrings");
public string FilePath => _configRecord.ConfigurationFilePath;
public bool HasFile => _configRecord.HasStream;
public ConfigurationLocationCollection Locations => _locations ?? (_locations = _configRecord.GetLocationCollection(this));
public ContextInformation EvaluationContext => _evalContext ?? (_evalContext = new ContextInformation(_configRecord));
public ConfigurationSectionGroup RootSectionGroup {
get {
if (_rootSectionGroup == null) {
_rootSectionGroup = new ConfigurationSectionGroup();
_rootSectionGroup.RootAttachToConfigurationRecord(_configRecord);
}
return _rootSectionGroup;
}
}
public ConfigurationSectionCollection Sections => RootSectionGroup.Sections;
public ConfigurationSectionGroupCollection SectionGroups => RootSectionGroup.SectionGroups;
public bool NamespaceDeclared {
get {
return _configRecord.NamespacePresent;
}
set {
_configRecord.NamespacePresent = value;
}
}
public Func<string, string> TypeStringTransformer {
get {
return _typeStringTransformer;
}
set {
if (_typeStringTransformer != value) {
TypeStringTransformerIsSet = (value != null);
_typeStringTransformer = value;
}
}
}
public Func<string, string> AssemblyStringTransformer {
get {
return _assemblyStringTransformer;
}
set {
if (_assemblyStringTransformer != value) {
AssemblyStringTransformerIsSet = (value != null);
_assemblyStringTransformer = value;
}
}
}
public FrameworkName TargetFramework { get; set; }
internal bool TypeStringTransformerIsSet { get; set; }
internal bool AssemblyStringTransformerIsSet { get; set; }
internal Stack SectionsStack => _sectionsStack ?? (_sectionsStack = new Stack());
internal Configuration(string locationSubPath, Type typeConfigHost, params object[] hostInitConfigurationParams)
{
_typeConfigHost = typeConfigHost;
_hostInitConfigurationParams = hostInitConfigurationParams;
IInternalConfigHost host = (IInternalConfigHost)TypeUtil.CreateInstance(typeConfigHost);
UpdateConfigHost host2 = new UpdateConfigHost(host);
IInternalConfigHost internalConfigHost = new ImplicitMachineConfigHost(host2);
InternalConfigRoot internalConfigRoot = new InternalConfigRoot(this, host2);
((IInternalConfigRoot)internalConfigRoot).Init(internalConfigHost, true);
internalConfigHost.InitForConfiguration(ref locationSubPath, out string configPath, out string locationConfigPath, internalConfigRoot, hostInitConfigurationParams);
if (!string.IsNullOrEmpty(locationSubPath) && !internalConfigHost.SupportsLocation)
throw ExceptionUtil.UnexpectedError("Configuration::ctor");
if (string.IsNullOrEmpty(locationSubPath) != string.IsNullOrEmpty(locationConfigPath))
throw ExceptionUtil.UnexpectedError("Configuration::ctor");
_configRecord = (MgmtConfigurationRecord)internalConfigRoot.GetConfigRecord(configPath);
if (!string.IsNullOrEmpty(locationSubPath))
_configRecord = MgmtConfigurationRecord.Create(internalConfigRoot, _configRecord, locationConfigPath, locationSubPath);
_configRecord.ThrowIfInitErrors();
}
internal Configuration OpenLocationConfiguration(string locationSubPath)
{
return new Configuration(locationSubPath, _typeConfigHost, _hostInitConfigurationParams);
}
public ConfigurationSection GetSection(string sectionName)
{
return (ConfigurationSection)_configRecord.GetSection(sectionName);
}
public ConfigurationSectionGroup GetSectionGroup(string sectionGroupName)
{
return _configRecord.GetSectionGroup(sectionGroupName);
}
public void Save()
{
SaveAsImpl(null, ConfigurationSaveMode.Modified, false);
}
public void Save(ConfigurationSaveMode saveMode)
{
SaveAsImpl(null, saveMode, false);
}
public void Save(ConfigurationSaveMode saveMode, bool forceSaveAll)
{
SaveAsImpl(null, saveMode, forceSaveAll);
}
public void SaveAs(string filename)
{
SaveAs(filename, ConfigurationSaveMode.Modified, false);
}
public void SaveAs(string filename, ConfigurationSaveMode saveMode)
{
SaveAs(filename, saveMode, false);
}
public void SaveAs(string filename, ConfigurationSaveMode saveMode, bool forceSaveAll)
{
if (string.IsNullOrEmpty(filename))
throw ExceptionUtil.ParameterNullOrEmpty("filename");
SaveAsImpl(filename, saveMode, forceSaveAll);
}
private void SaveAsImpl(string filename, ConfigurationSaveMode saveMode, bool forceSaveAll)
{
filename = (string.IsNullOrEmpty(filename) ? null : Path.GetFullPath(filename));
if (forceSaveAll)
ForceGroupsRecursive(RootSectionGroup);
_configRecord.SaveAs(filename, saveMode, forceSaveAll);
}
private void ForceGroupsRecursive(ConfigurationSectionGroup group)
{
foreach (ConfigurationSection section in group.Sections) {
ConfigurationSection configurationSection2 = group.Sections[section.SectionInformation.Name];
}
foreach (ConfigurationSectionGroup sectionGroup in group.SectionGroups) {
ForceGroupsRecursive(group.SectionGroups[sectionGroup.Name]);
}
}
}
}