备忘:
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="fileSystemStorageProvider" type="ConfigurationDemo.FileSystemStorageSettings, ConfigurationDemo" />
<section name="fileSystemStorageProviderOther" type="ConfigurationDemo.FileSystemStorageProviderSectionHandler, ConfigurationDemo" />
</configSections>
<fileSystemStorageProvider Root="D:\temp\FileStorage\Core" FolderPattern="yyyy\MM\dd" ApDocConverterTimeout="300">
</fileSystemStorageProvider>
<fileSystemStorageProviderOther>
<param name="Root" value="D:\temp\FileStorage\Core" />
<param name="FolderPattern" value="yyyy\MM\dd" />
<param name="apDocConverterTimeout" value="300" />
</fileSystemStorageProviderOther>
</configuration>
<configuration>
<configSections>
<section name="fileSystemStorageProvider" type="ConfigurationDemo.FileSystemStorageSettings, ConfigurationDemo" />
<section name="fileSystemStorageProviderOther" type="ConfigurationDemo.FileSystemStorageProviderSectionHandler, ConfigurationDemo" />
</configSections>
<fileSystemStorageProvider Root="D:\temp\FileStorage\Core" FolderPattern="yyyy\MM\dd" ApDocConverterTimeout="300">
</fileSystemStorageProvider>
<fileSystemStorageProviderOther>
<param name="Root" value="D:\temp\FileStorage\Core" />
<param name="FolderPattern" value="yyyy\MM\dd" />
<param name="apDocConverterTimeout" value="300" />
</fileSystemStorageProviderOther>
</configuration>
读配置文件:
1,
类:FileSystemStorageSettings
using System.Configuration;
namespace ConfigurationDemo
{
public class FileSystemStorageSettings : ConfigurationSection
{
[ConfigurationProperty("Root")]
public string Root
{
get { return (string)this["Root"]; }
set
{
this["Root"] = value;
}
}
[ConfigurationProperty("FolderPattern")]
public string FolderPattern
{
get { return (string)this["FolderPattern"]; }
set
{
this["FolderPattern"] = value;
}
}
[ConfigurationProperty("ApDocConverterTimeout")]
public short ApDocConverterTimeout
{
get { return (short)this["ApDocConverterTimeout"]; }
set
{
this["ApDocConverterTimeout"] = value;
}
}
}
}
namespace ConfigurationDemo
{
public class FileSystemStorageSettings : ConfigurationSection
{
[ConfigurationProperty("Root")]
public string Root
{
get { return (string)this["Root"]; }
set
{
this["Root"] = value;
}
}
[ConfigurationProperty("FolderPattern")]
public string FolderPattern
{
get { return (string)this["FolderPattern"]; }
set
{
this["FolderPattern"] = value;
}
}
[ConfigurationProperty("ApDocConverterTimeout")]
public short ApDocConverterTimeout
{
get { return (short)this["ApDocConverterTimeout"]; }
set
{
this["ApDocConverterTimeout"] = value;
}
}
}
}
FileSystemStorageSettings setting = ConfigurationManager.GetSection("fileSystemStorageProvider") as FileSystemStorageSettings;
if(setting != null)
{
//...
}
if(setting != null)
{
//...
}
2,
类:
FileSystemStorageSettingsOther
using System;
using System.Configuration;
using System.Xml;
namespace ConfigurationDemo
{
public class FileSystemStorageSettingsOther
{
private readonly string root;
private readonly string folderPattern;
private readonly short apDocConverterTimeout;
public FileSystemStorageSettingsOther(string root, string folderPattern, string apDocConverterTimeout)
{
this.root = root;
this.folderPattern = folderPattern;
if (!short.TryParse(apDocConverterTimeout, out this.apDocConverterTimeout))
this.apDocConverterTimeout = 30;
}
public string Root
{
get {return root;}
}
public string FolderPattern
{
get {return folderPattern;}
}
public short ApDocConverterTimeout
{
get { return apDocConverterTimeout; }
}
}
public class FileSystemStorageProviderSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
try
{
XmlElement rootNode = (XmlElement)section.SelectSingleNode("param[@name='Root']");
string root = rootNode.Attributes["value"].InnerText;
XmlElement folderPatternNode = (XmlElement)section.SelectSingleNode("param[@name='FolderPattern']");
string folderPattern = folderPatternNode.Attributes["value"].InnerText;
XmlElement apDocConverterTimeoutNode = (XmlElement)section.SelectSingleNode("param[@name='apDocConverterTimeout']");
string apDocConverterTimeout;
if (apDocConverterTimeoutNode == null)
apDocConverterTimeout = null;
else
apDocConverterTimeout = apDocConverterTimeoutNode.Attributes["value"].InnerText;
return new FileSystemStorageSettingsOther(root, folderPattern, apDocConverterTimeout);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
using System.Configuration;
using System.Xml;
namespace ConfigurationDemo
{
public class FileSystemStorageSettingsOther
{
private readonly string root;
private readonly string folderPattern;
private readonly short apDocConverterTimeout;
public FileSystemStorageSettingsOther(string root, string folderPattern, string apDocConverterTimeout)
{
this.root = root;
this.folderPattern = folderPattern;
if (!short.TryParse(apDocConverterTimeout, out this.apDocConverterTimeout))
this.apDocConverterTimeout = 30;
}
public string Root
{
get {return root;}
}
public string FolderPattern
{
get {return folderPattern;}
}
public short ApDocConverterTimeout
{
get { return apDocConverterTimeout; }
}
}
public class FileSystemStorageProviderSectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
try
{
XmlElement rootNode = (XmlElement)section.SelectSingleNode("param[@name='Root']");
string root = rootNode.Attributes["value"].InnerText;
XmlElement folderPatternNode = (XmlElement)section.SelectSingleNode("param[@name='FolderPattern']");
string folderPattern = folderPatternNode.Attributes["value"].InnerText;
XmlElement apDocConverterTimeoutNode = (XmlElement)section.SelectSingleNode("param[@name='apDocConverterTimeout']");
string apDocConverterTimeout;
if (apDocConverterTimeoutNode == null)
apDocConverterTimeout = null;
else
apDocConverterTimeout = apDocConverterTimeoutNode.Attributes["value"].InnerText;
return new FileSystemStorageSettingsOther(root, folderPattern, apDocConverterTimeout);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
调用:
FileSystemStorageSettingsOther setting = ConfigurationManager.GetSection("fileSystemStorageProviderOther") as FileSystemStorageSettingsOther;
if(setting != null)
{
//...
}
if(setting != null)
{
//...
}
写入配置文件:
1,
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Remove("EditorSettings");
config.Sections.Add("EditorSettings", configData);
config.Save();
config.Sections.Remove("EditorSettings");
config.Sections.Add("EditorSettings", configData);
config.Save();
2, ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"E:\EntLib3Src\Quick Starts\Configuration-Migration\CS\ConfigurationMigrationQuickStart\app.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
config.Sections.Remove("EditorSettings");
config.Sections.Add("EditorSettings", configData);
config.Save();