C# 带有单独的.config文件的System.Configuration
我从这个示例中获取了代码 我想知道的是(今天我在网上到处寻找)。。。 如何将其保存在外部(单独)文件中 我的想法是:C# 带有单独的.config文件的System.Configuration,c#,configuration-files,C#,Configuration Files,我从这个示例中获取了代码 我想知道的是(今天我在网上到处寻找)。。。 如何将其保存在外部(单独)文件中 我的想法是: <configuration> <configSections> <sectionGroup name="pageAppearanceGroup"> <section name="pageAppearance" type="HelperAssembly.Configuration.
<configuration>
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="HelperAssembly.Configuration.PageAppearanceSection,HelperAssembly"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
<pageAppearanceGroup fileName="SomeSeparateFile.config"/>
</configuration>
上述方法(当然)不起作用
下面是我对上面提到的ms文章的复制/粘贴。
当我把它贴在这里时,它已经完全发挥作用了
//START HelperAssembly.csproj
namespace HelperAssembly.Configuration
{
using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml;
public class PageAppearanceSection : ConfigurationSection
{
// Create a "remoteOnly" attribute.
[ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
public Boolean RemoteOnly
{
get
{
return (Boolean)this["remoteOnly"];
}
set
{
this["remoteOnly"] = value;
}
}
// Create a "font" element.
[ConfigurationProperty("font")]
public FontElement Font
{
get
{
return (FontElement)this["font"];
}
set
{ this["font"] = value; }
}
// Create a "color element."
[ConfigurationProperty("color")]
public ColorElement Color
{
get
{
return (ColorElement)this["color"];
}
set
{ this["color"] = value; }
}
}
// Define the "font" element
// with "name" and "size" attributes.
public class FontElement : ConfigurationElement
{
[ConfigurationProperty("name", DefaultValue = "Arial", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public String Name
{
get
{
return (String)this["name"];
}
set
{
this["name"] = value;
}
}
[ConfigurationProperty("size", DefaultValue = "12", IsRequired = false)]
[IntegerValidator(ExcludeRange = false, MaxValue = 24, MinValue = 6)]
public int Size
{
get
{ return (int)this["size"]; }
set
{ this["size"] = value; }
}
}
// Define the "color" element
// with "background" and "foreground" attributes.
public class ColorElement : ConfigurationElement
{
[ConfigurationProperty("background", DefaultValue = "FFFFFF", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
public String Background
{
get
{
return (String)this["background"];
}
set
{
this["background"] = value;
}
}
[ConfigurationProperty("foreground", DefaultValue = "000000", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)]
public String Foreground
{
get
{
return (String)this["foreground"];
}
set
{
this["foreground"] = value;
}
}
}
}
namespace HelperAssembly.Configuration
{
using System;
using System.Configuration;
public static class ConfigurationRetriever
{
public static PageAppearanceSection RetrievePageAppearanceSection1()
{
PageAppearanceSection config = (PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");
return config;
}
}
}
//START ConsoleApplication1.csproj
using System;
using HelperAssembly.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
PageAppearanceSection pas = ConfigurationRetriever.RetrievePageAppearanceSection1();
if (null != pas)
{
Console.WriteLine(pas.Color.Foreground);
Console.WriteLine(pas.Color.Background);
}
}
catch (Exception ex)
{
Exception innerException = ex;
while (null != innerException)
{
Console.WriteLine(innerException.Message);
Console.WriteLine("\n\r");
Console.WriteLine(innerException.StackTrace);
Console.WriteLine("\n\r");
innerException = innerException.InnerException;
}
}
Console.WriteLine("Press Enter");
Console.ReadLine();
}
}
}
//XML for config file that works with the above code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="HelperAssembly.Configuration.PageAppearanceSection,HelperAssembly"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
<pageAppearanceGroup>
<pageAppearance remoteOnly="true">
<font name="TimesNewRoman" size="18"/>
<color background="DEFDEF" foreground="ABCABC"/>
</pageAppearance>
</pageAppearanceGroup>
</configuration>
//启动HelperAssembly.csproj
命名空间HelperAssembly.Configuration
{
使用制度;
使用系统集合;
使用系统文本;
使用系统配置;
使用System.Xml;
公共类页面外观部分:配置部分
{
//创建一个“remoteOnly”属性。
[ConfigurationProperty(“remoteOnly”,DefaultValue=“false”,IsRequired=false)]
仅限公共布尔值
{
得到
{
返回(布尔值)此[“remoteOnly”];
}
设置
{
此[“remoteOnly”]=值;
}
}
//创建一个“字体”元素。
[配置属性(“字体”)]
公共字体元素字体
{
得到
{
返回(FontElement)此[“字体”];
}
设置
{this[“font”]=value;}
}
//创建一个“颜色元素”
[配置属性(“颜色”)]
公共颜色元素颜色
{
得到
{
返回(ColorElement)此[“颜色”];
}
设置
{this[“color”]=值;}
}
}
//定义“字体”元素
//具有“名称”和“大小”属性。
公共类FontElement:ConfigurationElement
{
[ConfigurationProperty(“name”,DefaultValue=“Arial”,IsRequired=true)]
[StringValidator(InvalidCharacters=“~!@$%^&*()[]{}/;”\“\”,MinLength=1,MaxLength=60)]
公共字符串名
{
得到
{
返回(字符串)此[“名称”];
}
设置
{
此[“名称”]=值;
}
}
[ConfigurationProperty(“大小”,DefaultValue=“12”,IsRequired=false)]
[IntegerValidator(ExcludeRange=false,MaxValue=24,MinValue=6)]
公共整数大小
{
得到
{return(int)this[“size”];}
设置
{this[“size”]=value;}
}
}
//定义“颜色”元素
//具有“背景”和“前景”属性。
公共类ColorElement:ConfigurationElement
{
[ConfigurationProperty(“背景”,DefaultValue=“FFFFFF”,IsRequired=true)]
[StringValidator(InvalidCharacters=“~!@$%^&*()[]{}/;”\“\\GHIJKLMNOPQRSTUVWXYZ”,MinLength=6,MaxLength=6)]
公共字符串背景
{
得到
{
返回(字符串)此[“背景”];
}
设置
{
此[“背景”]=值;
}
}
[ConfigurationProperty(“前台”,DefaultValue=“000000”,IsRequired=true)]
[StringValidator(InvalidCharacters=“~!@$%^&*()[]{}/;”\“\\GHIJKLMNOPQRSTUVWXYZ”,MinLength=6,MaxLength=6)]
公共字符串前景
{
得到
{
返回(字符串)此[“前台”];
}
设置
{
此[“前景”]=值;
}
}
}
}
命名空间HelperAssembly.Configuration
{
使用制度;
使用系统配置;
公共静态类配置检索器
{
公共静态页面外观部分检索页面外观部分1()
{
PageAppearanceSection config=(PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection(“pageAppearanceGroup/pageAppearance”);
返回配置;
}
}
}
//启动控制台应用程序1.csproj
使用制度;
使用HelperAssembly.Configuration;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
PageAppearanceSection pas=配置检索器。检索PageAppearanceSection 1();
如果(null!=pas)
{
控制台。写入线(pas。颜色。前景);
控制台。写入线(pas。颜色。背景);
}
}
捕获(例外情况除外)
{
异常innerException=ex;
while(null!=innerException)
{
Console.WriteLine(innerException.Message);
Console.WriteLine(“\n\r”);
Console.WriteLine(innerException.StackTrace);
Console.WriteLine(“\n\r”);
innerException=innerException.innerException;
}
}
控制台。写入线(“按回车键”);
Console.ReadLine();
}
}
}
//用于配置文件的XML,用于处理上述代码
尝试使用configSource
如果您将app.config更改为使用此选项,则此选项将起作用:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="pageAppearanceGroup">
<section
name="pageAppearance"
type="HelperAssembly.Configuration.PageAppearanceSection,HelperAssembly"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
<pageAppearanceGroup>
<pageAppearance configSource="SomeSeparateFile.config"/>
</pageAppearanceGroup>
</configuration>
您的someSeparateFile.config如下所示:
<pageAppearance remoteOnly="true">
<font name="TimesNewRoman" size="18"/>
<color background="123456" foreground="ABCDEF"/>
</pageAppearance>
(此文件中没有配置元素!)
我已经能够将configSections移动到单独的文件中。除非您进行更多的编程,否则不确定您是否可以使用configGroups执行此操作。通过配置框架模型,您可以非常轻松地将configSections移出
希望这会有所帮助!!要在此网站上包含代码和/或XML,请在编辑器中选择它,然后按Control-K.BTW,