gpt4 book ai didi

c# - 带有单独 .config 文件的 System.Configuration

转载 作者:太空狗 更新时间:2023-10-30 00:06:25 24 4
gpt4 key购买 nike

我从这个例子中获取了代码。

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

我想知道的是(我今天在网上到处找)......你如何把它放在一个外部(单独的)文件中。

我的想法是:

<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>

最佳答案

如果您更改 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 移出。

希望这对您有所帮助!

关于c# - 带有单独 .config 文件的 System.Configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3366199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com