在C#应用程序中添加自定义配置文件

时间:2022-09-02 15:17:20

Is it possible to create a custom configuration file (other than app.config) that can be processed by classes in the System.Configuration namespace?

是否可以创建可由System.Configuration命名空间中的类处理的自定义配置文件(app.config除外)?

I have seen a ton of articles that talk about custom sections (inside the app.config file) but I would like to make an entirely new config file.

我看过很多关于自定义部分的文章(在app.config文件中),但我想创建一个全新的配置文件。

Is there any decent documentation that covers this topic?

有没有关于这个主题的体面文档?

thanks

1 个解决方案

#1


You do not state what you want to achieve by having a separate file, but there are a couple of different things you can do.

您没有通过单独的文件说明您想要实现的目标,但您可以执行几项不同的操作。

If you want to "modularize" you configuration, you can break out certain config sections to separate files, using the configSource attribute:

如果您想“模块化”您的配置,您可以使用configSource属性将某些配置节分解为单独的文件:

// point out a file containing the connectionStrings config section
<connectionStrings configSource="connections.config"></connectionStrings>

You can also open a specific configuration file by calling ConfigurationManager.OpenExeConfiguration.

您还可以通过调用ConfigurationManager.OpenExeConfiguration打开特定的配置文件。

#1


You do not state what you want to achieve by having a separate file, but there are a couple of different things you can do.

您没有通过单独的文件说明您想要实现的目标,但您可以执行几项不同的操作。

If you want to "modularize" you configuration, you can break out certain config sections to separate files, using the configSource attribute:

如果您想“模块化”您的配置,您可以使用configSource属性将某些配置节分解为单独的文件:

// point out a file containing the connectionStrings config section
<connectionStrings configSource="connections.config"></connectionStrings>

You can also open a specific configuration file by calling ConfigurationManager.OpenExeConfiguration.

您还可以通过调用ConfigurationManager.OpenExeConfiguration打开特定的配置文件。