C# 使用 App.config 设置强类型变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1772140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:41:40  来源:igfitidea点击:

Using App.config to set strongly-typed variables

c#.net-3.5configuration-filesapp-config

提问by Mass Dot Net

I'm a C# novice running .NET 3.5, and I'd like to store a bunch of application default values in App.config, as the settings may vary by server environment (e.g. development, staging, production). What I want to do is similar to what's described in this StackOverflow article, but I also want to be able to use non-string values (e.g. int, bool). Something like this (name-values are just examples, btw):

我是运行 .NET 3.5 的 C# 新手,我想在 App.config 中存储一堆应用程序默认值,因为设置可能因服务器环境(例如开发、登台、生产)而异。我想要做的类似于这篇 StackOverflow 文章中描述的内容,但我也希望能够使用非字符串值(例如 int、bool)。像这样的东西(名称值只是示例,顺便说一句):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <applicationSettings>
        <MyApp>
            <setting name="InitText" serializeAs="String">
                <value>Hello</value>
            </setting>
            <setting name="StartAt" serializeAs="Integer">
                <value>5</value>
            </setting>
            <setting name="IsWeekend" serializeAs="Boolean">
                <value>True</value>
            </setting>
        </MyApp>
    </applicationSettings>
</configuration>

Could somebody provide an example of how to do this, and how to retrieve the values via C#? I've seen a lot of examples that require using and , but I'm not sure if I need those elements, and if so, how to create them.

有人可以提供一个如何执行此操作以及如何通过 C# 检索值的示例吗?我看过很多需要使用 and 的例子,但我不确定我是否需要这些元素,如果需要,如何创建它们。

采纳答案by Jo?o Angelo

What about using the Application Settings architectureof the .NET Framework. You can have strongly typed access to the default values.

如何使用.NET Framework的应用程序设置架构。您可以对默认值进行强类型访问。

On a Windows Application project the Settings file is created automatically in the Resources folder. You can then add application settings that are persisted in the App.config file just as you showed in your question.

在 Windows 应用程序项目中,设置文件是在资源文件夹中自动创建的。然后,您可以添加保留在 App.config 文件中的应用程序设置,就像您在问题中显示的那样。

For example:

例如:

int i = Settings.Default.IntSetting;

bool b = Settings.Default.BoolSetting;

Edit:If your project does not contain the settings file you can always add one by adding a New Item and then choosing a Settings File. (Right click project file and do: Add->New Item->Settings File). In my case I named it Settings but you can name it whatever you want.

编辑:如果您的项目不包含设置文件,您始终可以通过添加一个新项目然后选择一个设置文件来添加一个。(右键单击项目文件并执行:添加->新建项目->设置文件)。就我而言,我将其命名为 Settings,但您可以随意命名。

After adding the file visual studio will open the settings designer in which you can add your strongly-typed settings. From what you said you should set the settings at the Application scope and not at the user. Then build the project and you should get access to a class with the name of the file.

添加文件后,visual studio 将打开设置设计器,您可以在其中添加强类型设置。根据您所说的,您应该在应用程序范围内而不是在用户上设置设置。然后构建项目,您应该可以访问具有文件名的类。

回答by JayG

Boolean isWeekend = Convert.ToBoolean(ConfigurationManager.AppSettings["IsWeekend"])

回答by tobsen

The thing JayG suggested can be done by the Visual Studio automatically. Use the appsettings wizard as described in the MSDN. Also the whole Application Settingsinfrastructure might be worth a read.

JayG 建议的事情可以由 Visual Studio 自动完成。使用MSDN 中所述的应用设置向导。此外,整个应用程序设置基础设施可能值得一读。

回答by jvenema

Sounds like you want to have a custom <configSection>. Basically, it works like this:

听起来您想要自定义<configSection>. 基本上,它是这样工作的:

-Create a configuration class that has proper default values, etc, and set up a nice inheritance structure so it'll auto-load based on your web.config settings. -Add your config section to the web.config, specifying the "type" that will load your settings so the .NET framework will know what to initialize

- 创建一个具有适当默认值等的配置类,并设置一个很好的继承结构,以便它会根据您的 web.config 设置自动加载。- 将您的配置部分添加到 web.config,指定将加载您的设置的“类型”,以便 .NET 框架知道要初始化的内容

You can find all kinds of info on the MSDN libraryabout it. The examples are asp.net, but they work just fine with app configs as well.

您可以在MSDN 库上找到有关它的各种信息。这些示例是 asp.net,但它们也适用于应用程序配置。