LocalFileSettingsProvider
Provides persistence for application settings classes.
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
namespace System.Configuration
{
public class LocalFileSettingsProvider : SettingsProvider, IApplicationSettingsProvider
{
private sealed class XmlEscaper
{
private readonly XmlDocument document;
private readonly XmlElement tempElement;
internal XmlEscaper()
{
document = new XmlDocument();
tempElement = document.CreateElement("temp");
}
internal string Escape(string xmlString)
{
if (string.IsNullOrEmpty(xmlString))
return xmlString;
tempElement.InnerText = xmlString;
return tempElement.InnerXml;
}
internal string Unescape(string escapedString)
{
if (string.IsNullOrEmpty(escapedString))
return escapedString;
tempElement.InnerXml = escapedString;
return tempElement.InnerText;
}
}
private string _appName = string.Empty;
private ClientSettingsStore _store;
private string _prevLocalConfigFileName;
private string _prevRoamingConfigFileName;
private XmlEscaper _escaper;
public override string ApplicationName {
get {
return _appName;
}
set {
_appName = value;
}
}
private XmlEscaper Escaper => _escaper ?? (_escaper = new XmlEscaper());
private ClientSettingsStore Store => _store ?? (_store = new ClientSettingsStore());
public override void Initialize(string name, NameValueCollection values)
{
if (string.IsNullOrEmpty(name))
name = "LocalFileSettingsProvider";
base.Initialize(name, values);
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
{
SettingsPropertyValueCollection settingsPropertyValueCollection = new SettingsPropertyValueCollection();
string sectionName = GetSectionName(context);
IDictionary dictionary = ClientSettingsStore.ReadSettings(sectionName, false);
IDictionary dictionary2 = ClientSettingsStore.ReadSettings(sectionName, true);
ConnectionStringSettingsCollection connectionStringSettingsCollection = ClientSettingsStore.ReadConnectionStrings();
foreach (SettingsProperty property in properties) {
string name = property.Name;
SettingsPropertyValue settingsPropertyValue = new SettingsPropertyValue(property);
SpecialSettingAttribute specialSettingAttribute = property.Attributes[typeof(SpecialSettingAttribute)] as SpecialSettingAttribute;
if (specialSettingAttribute != null && specialSettingAttribute.SpecialSetting == SpecialSetting.ConnectionString) {
string name2 = sectionName + "." + name;
if (connectionStringSettingsCollection != null && connectionStringSettingsCollection[name2] != null)
settingsPropertyValue.PropertyValue = connectionStringSettingsCollection[name2].ConnectionString;
else if (property.DefaultValue != null && property.DefaultValue is string) {
settingsPropertyValue.PropertyValue = property.DefaultValue;
} else {
settingsPropertyValue.PropertyValue = string.Empty;
}
settingsPropertyValue.IsDirty = false;
settingsPropertyValueCollection.Add(settingsPropertyValue);
} else {
bool flag = IsUserSetting(property);
if (flag && !ConfigurationManagerInternalFactory.Instance.SupportsUserConfig)
throw new ConfigurationErrorsException(System.SR.UserSettingsNotSupported);
IDictionary dictionary3 = flag ? dictionary2 : dictionary;
if (dictionary3.Contains(name)) {
StoredSetting storedSetting = (StoredSetting)dictionary3[name];
string text = storedSetting.Value.InnerXml;
if (storedSetting.SerializeAs == SettingsSerializeAs.String)
text = Escaper.Unescape(text);
settingsPropertyValue.SerializedValue = text;
} else if (property.DefaultValue != null) {
settingsPropertyValue.SerializedValue = property.DefaultValue;
} else {
settingsPropertyValue.PropertyValue = null;
}
settingsPropertyValue.IsDirty = false;
settingsPropertyValueCollection.Add(settingsPropertyValue);
}
}
return settingsPropertyValueCollection;
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
{
string sectionName = GetSectionName(context);
IDictionary dictionary = new Hashtable();
IDictionary dictionary2 = new Hashtable();
foreach (SettingsPropertyValue value in values) {
SettingsProperty property = value.Property;
bool flag = IsUserSetting(property);
if (value.IsDirty && flag) {
bool flag2 = IsRoamingSetting(property);
StoredSetting storedSetting = new StoredSetting(property.SerializeAs, SerializeToXmlElement(property, value));
if (flag2)
dictionary[property.Name] = storedSetting;
else
dictionary2[property.Name] = storedSetting;
value.IsDirty = false;
}
}
if (dictionary.Count > 0)
ClientSettingsStore.WriteSettings(sectionName, true, dictionary);
if (dictionary2.Count > 0)
ClientSettingsStore.WriteSettings(sectionName, false, dictionary2);
}
public void Reset(SettingsContext context)
{
string sectionName = GetSectionName(context);
ClientSettingsStore.RevertToParent(sectionName, true);
ClientSettingsStore.RevertToParent(sectionName, false);
}
public void Upgrade(SettingsContext context, SettingsPropertyCollection properties)
{
SettingsPropertyCollection settingsPropertyCollection = new SettingsPropertyCollection();
SettingsPropertyCollection settingsPropertyCollection2 = new SettingsPropertyCollection();
foreach (SettingsProperty property in properties) {
if (IsRoamingSetting(property))
settingsPropertyCollection2.Add(property);
else
settingsPropertyCollection.Add(property);
}
if (settingsPropertyCollection2.Count > 0)
Upgrade(context, settingsPropertyCollection2, true);
if (settingsPropertyCollection.Count > 0)
Upgrade(context, settingsPropertyCollection, false);
}
public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty property)
{
bool isRoaming = IsRoamingSetting(property);
string previousConfigFileName = GetPreviousConfigFileName(isRoaming);
if (!string.IsNullOrEmpty(previousConfigFileName)) {
SettingsPropertyCollection settingsPropertyCollection = new SettingsPropertyCollection();
settingsPropertyCollection.Add(property);
SettingsPropertyValueCollection settingValuesFromFile = GetSettingValuesFromFile(previousConfigFileName, GetSectionName(context), true, settingsPropertyCollection);
return settingValuesFromFile[property.Name];
}
SettingsPropertyValue settingsPropertyValue = new SettingsPropertyValue(property);
settingsPropertyValue.PropertyValue = null;
return settingsPropertyValue;
}
private string GetPreviousConfigFileName(bool isRoaming)
{
if (!ConfigurationManagerInternalFactory.Instance.SupportsUserConfig)
throw new ConfigurationErrorsException(System.SR.UserSettingsNotSupported);
string text = isRoaming ? _prevRoamingConfigFileName : _prevLocalConfigFileName;
if (string.IsNullOrEmpty(text)) {
string path = isRoaming ? ConfigurationManagerInternalFactory.Instance.ExeRoamingConfigDirectory : ConfigurationManagerInternalFactory.Instance.ExeLocalConfigDirectory;
if (!Version.TryParse(ConfigurationManagerInternalFactory.Instance.ExeProductVersion, out Version result))
return null;
Version version = null;
DirectoryInfo directoryInfo = null;
string text2 = null;
DirectoryInfo parent = Directory.GetParent(path);
if (parent.Exists) {
DirectoryInfo[] directories = parent.GetDirectories();
foreach (DirectoryInfo directoryInfo2 in directories) {
if (Version.TryParse(directoryInfo2.Name, out Version result2) && result2 < result) {
if (version == (Version)null) {
version = result2;
directoryInfo = directoryInfo2;
} else if (result2 > version) {
version = result2;
directoryInfo = directoryInfo2;
}
}
}
if (directoryInfo != null)
text2 = Path.Combine(directoryInfo.FullName, ConfigurationManagerInternalFactory.Instance.UserConfigFilename);
if (File.Exists(text2))
text = text2;
}
if (isRoaming)
_prevRoamingConfigFileName = text;
else
_prevLocalConfigFileName = text;
}
return text;
}
private static string GetSectionName(SettingsContext context)
{
string text = (string)context["GroupName"];
string text2 = (string)context["SettingsKey"];
string text3 = text;
if (!string.IsNullOrEmpty(text2))
text3 = text3 + "." + text2;
return XmlConvert.EncodeLocalName(text3);
}
private SettingsPropertyValueCollection GetSettingValuesFromFile(string configFileName, string sectionName, bool userScoped, SettingsPropertyCollection properties)
{
SettingsPropertyValueCollection settingsPropertyValueCollection = new SettingsPropertyValueCollection();
IDictionary dictionary = ClientSettingsStore.ReadSettingsFromFile(configFileName, sectionName, userScoped);
foreach (SettingsProperty property in properties) {
string name = property.Name;
SettingsPropertyValue settingsPropertyValue = new SettingsPropertyValue(property);
if (dictionary.Contains(name)) {
StoredSetting storedSetting = (StoredSetting)dictionary[name];
string text = storedSetting.Value.InnerXml;
if (storedSetting.SerializeAs == SettingsSerializeAs.String)
text = Escaper.Unescape(text);
settingsPropertyValue.SerializedValue = text;
settingsPropertyValue.IsDirty = true;
settingsPropertyValueCollection.Add(settingsPropertyValue);
}
}
return settingsPropertyValueCollection;
}
private static bool IsRoamingSetting(SettingsProperty setting)
{
SettingsManageabilityAttribute settingsManageabilityAttribute = setting.Attributes[typeof(SettingsManageabilityAttribute)] as SettingsManageabilityAttribute;
if (settingsManageabilityAttribute != null)
return (settingsManageabilityAttribute.Manageability & SettingsManageability.Roaming) == SettingsManageability.Roaming;
return false;
}
private static bool IsUserSetting(SettingsProperty setting)
{
bool flag = setting.Attributes[typeof(UserScopedSettingAttribute)] is UserScopedSettingAttribute;
bool flag2 = setting.Attributes[typeof(ApplicationScopedSettingAttribute)] is ApplicationScopedSettingAttribute;
if (flag & flag2)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.BothScopeAttributes, setting.Name));
if (!(flag | flag2))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.NoScopeAttributes, setting.Name));
return flag;
}
private XmlElement SerializeToXmlElement(SettingsProperty setting, SettingsPropertyValue value)
{
XmlDocument xmlDocument = new XmlDocument();
XmlElement xmlElement = xmlDocument.CreateElement("value");
string text = value.SerializedValue as string;
if (text == null && setting.SerializeAs == SettingsSerializeAs.Binary) {
byte[] array = value.SerializedValue as byte[];
if (array != null)
text = Convert.ToBase64String(array);
}
if (text == null)
text = string.Empty;
if (setting.SerializeAs == SettingsSerializeAs.String)
text = Escaper.Escape(text);
xmlElement.InnerXml = text;
XmlNode xmlNode = null;
foreach (XmlNode childNode in xmlElement.ChildNodes) {
if (childNode.NodeType == XmlNodeType.XmlDeclaration) {
xmlNode = childNode;
break;
}
}
if (xmlNode != null)
xmlElement.RemoveChild(xmlNode);
return xmlElement;
}
private void Upgrade(SettingsContext context, SettingsPropertyCollection properties, bool isRoaming)
{
string previousConfigFileName = GetPreviousConfigFileName(isRoaming);
if (!string.IsNullOrEmpty(previousConfigFileName)) {
SettingsPropertyCollection settingsPropertyCollection = new SettingsPropertyCollection();
foreach (SettingsProperty property in properties) {
if (!(property.Attributes[typeof(NoSettingsVersionUpgradeAttribute)] is NoSettingsVersionUpgradeAttribute))
settingsPropertyCollection.Add(property);
}
SettingsPropertyValueCollection settingValuesFromFile = GetSettingValuesFromFile(previousConfigFileName, GetSectionName(context), true, settingsPropertyCollection);
SetPropertyValues(context, settingValuesFromFile);
}
}
}
}