📄 configurationsettings.cs
字号:
using System;
using System.Collections.Specialized;
using System.Xml;
namespace Addot.Configuration
{
/// <summary>
/// System.Configuration implementation for the .Net Compact Framework.
/// Please, refer to the .Net framework for the complete documentation.
/// </summary>
public class ConfigurationSettings
{
#region --- Fields ---
private enum ConfigAccess {Read, Write};
private static NameValueCollection appSettings;
private static string path;
#endregion
static ConfigurationSettings()
{
appSettings = null;
path = string.Empty;
}
#region --- Properties ---
public static NameValueCollection AppSettings
{
get
{
if( path == string.Empty )
{
path = System.Reflection.Assembly.GetCallingAssembly().GetName().CodeBase + ".config";
}
if( ( appSettings == null ) || ( appSettings.AllKeys.Length == 0 ) )
{
appSettings = new NameValueCollection(0);
XmlDocument xmlDocument = InitializeDocument();
ProcessXmlSettings(xmlDocument.DocumentElement, ConfigAccess.Read);
}
return appSettings;
}
}
#endregion
private static XmlDocument InitializeDocument()
{
System.IO.FileInfo infoConfig = new System.IO.FileInfo(path);
if( !infoConfig.Exists )
{
path = String.Empty;
System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Addot.Addot.Configuration", System.Reflection.Assembly.GetExecutingAssembly());
throw new ConfigurationException(string.Format(rm.GetString("UnexistingConfigFile"), path));
}
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
return xmlDocument;
}
private static void ProcessXmlSettings(XmlNode Node, ConfigAccess access)
{
if( ( Node != null ) && ( Node.Name.ToLower() == "appsettings" ) )
{
ProcessAppSettings(Node, access);
}
else if (Node.HasChildNodes)
{
foreach( XmlNode sub in Node.ChildNodes )
{
ProcessXmlSettings(sub, access);
}
}
}
private static void ProcessAppSettings(XmlNode parent, ConfigAccess access)
{
string key = String.Empty;
string val = String.Empty;
foreach( XmlNode node in parent.ChildNodes )
{
if( node.NodeType == XmlNodeType.Element )
{
foreach( XmlAttribute attr in node.Attributes )
{
if( access == ConfigAccess.Read )
{
switch( attr.Name.ToLower() )
{
case "key": key = attr.Value; break;
case "value": val = attr.Value; break;
default: break;
}
}
else
{
switch( attr.Name.ToLower() )
{
case "key": val = (string)appSettings.Get(attr.Value); break;
case "value": attr.Value = val; break;
default: break;
}
}
}
if( ( key != String.Empty ) && ( access == ConfigAccess.Read ) )
{
appSettings.Add(key, val);
}
}
}
}
public static void Save()
{
//
// Verify settings have been previously loaded
//
if( appSettings == null )
{
System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Addot.Addot.Configuration", System.Reflection.Assembly.GetExecutingAssembly());
throw new ConfigurationException(rm.GetString("ConfigSaveBeforeLoaded"));
}
XmlDocument xmlDocument = InitializeDocument();
ProcessXmlSettings(xmlDocument.DocumentElement, ConfigAccess.Write);
xmlDocument.Save(path);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -