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

发布于 2024-08-12 03:50:12 字数 1055 浏览 4 评论 0原文

我是运行.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>

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吾家有女初长成 2024-08-19 03:50:12

使用 .NET Framework 的应用程序设置体系结构怎么样?您可以对默认值进行强类型访问。

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

例如:

int i = Settings.Default.IntSetting;

bool b = Settings.Default.BoolSetting;

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

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

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

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.

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.

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.

我不吻晚风 2024-08-19 03:50:12
Boolean isWeekend = Convert.ToBoolean(ConfigurationManager.AppSettings["IsWeekend"])
Boolean isWeekend = Convert.ToBoolean(ConfigurationManager.AppSettings["IsWeekend"])
拥抱我好吗 2024-08-19 03:50:12

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

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 Settings infrastructure might be worth a read.

审判长 2024-08-19 03:50:12

听起来您想要一个自定义 。基本上,它的工作原理如下:

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

您可以在 MSDN 库<​​/a> 关于它。这些示例是 asp.net,但它们也可以与应用程序配置配合使用。

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

-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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文