如何从加载的App.config文件中检索ApplicationSettings? [英] How do I retrieve ApplicationSettings from a loaded App.config file?

查看:397
本文介绍了如何从加载的App.config文件中检索ApplicationSettings?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从加载的app.config文件的 applicationSettings 部分访问值?

Is it possible to access the values from the applicationSettings section of a loaded app.config file?

我找到一个示例< a href =http://stackoverflow.com/questions/1682054/how-do-i-retrieve-appsettings-from-the-assembly-config-file>如何检索 appSettings / a>,但我无法找到如何以这种方式访问​​ applicationSettings

推荐答案

在运行时, applicationSettings readonly 。您可以通过app.config文件中的文本编辑器直接设置/修改它们,但建议在Visual Studio中打开项目属性并选择设置选项卡。设置正确的范围很重要:

The applicationSettings are readonly during runtime. You can set/modify them either via a text editor in the app.config file directly, but it is recommended to open the project properties in Visual Studio and select the "Settings" tab. It is important to set the right scope:


  • 如果设置应用于整个应用程序(适用于所有用户),请选择应用程序范围。

  • 如果每个用户都应该有个人设置(绑定到用户个人资料),然后选择用户

例如,如果您在项目 WindowsFormsTestApplication1 中创建 myOwnSetting ,如下所示:

For example, if you create myOwnSetting in your project WindowsFormsTestApplication1 as follows:

它会将以下内容添加到应用程序的app.config文件:

it will add the following to the application's app.config file:

<applicationSettings>
  <WindowsFormsTestApplication1.Properties.Settings>
    <setting name="myOwnSetting" serializeAs="String">
      <value>Hi there!</value>
    </setting>
  </WindowsFormsTestApplication1.Properties.Settings>
</applicationSettings>

Visual Studio创建C#代码以自动访问此设置(这就是为什么你应该在项目中属性,而不是通过文本编辑器) - 保存更改后,您可以通过以下代码轻松地在应用程序中读取其值:

Visual Studio creates C# code to access this setting automatically (this is why you should do it in the project properties and not via text editor) - after you have saved the changes, you can read its value in the application easily via the following code:

var currentValue = Properties.Settings.Default.myOwnSetting;

鉴于上面列表中的 applicationSettings 这将检索字符串你好那里!对于变量 currentValue

Given the applicationSettings in the listing above, this would retrieve the string "Hi there!" for the variable currentValue.

注意,如果您已创建 myOwnSetting 用于用户范围,然后将其存储在名为< userSettings> 的节中,而不是< applicationSettings> ,但您仍然可以使用上面的代码行访问它。

Note that if you have created myOwnSetting for the "User" scope, then it is stored in a section named <userSettings> instead of <applicationSettings>, but you still can access it with the code line above.

范围User设置的另一个区别是您具有读写访问权限,即允许执行以下操作:

Another difference of scope "User" settings is that you have read-write access, i.e. it is allowed to do the following:

        Properties.Settings.Default.myUserSetting = "Something else";
        Properties.Settings.Default.Save();

如果你在Application范围设置myOwnSetting中使用同样的方法,

If you try the same with the "Application" scope setting myOwnSetting, it would result in a compile-time error telling you that it is read-only.

如果重新启动应用程序,您会注意到myUserSetting已更改为值Something other - 但是,旧值仍在app.config中。为什么会这样?原因是它被视为默认值 - 正如我前面所说,用户范围绑定到用户配置文件。因此,值其他存储在

If you re-start the application, you will notice that myUserSetting has changed to the value "Something else" - but the old value is still in the app.config. Why is this so? The reason is that it is regarded as a default value - and as I said earlier, the "User" scope is bound to the user profile. As a consequence, the value "Something else" is stored in

C:\Documents and Settings\USERID\Local Settings\Application Data\FIRMNAME\WindowsFormsTestApplicati_Url_tdq2oylz33rzq00sxhvxucu5edw2oghw\1.0.0.0

code> User.config ,其格式如下:

in a file named User.config, which looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <WindowsFormsTestApplication1.Properties.Settings>
            <setting name="myUserSetting" serializeAs="String">
                <value>Something else</value>
            </setting>
        </WindowsFormsTestApplication1.Properties.Settings>
    </userSettings>
</configuration>

您不能准确地知道路径是由.NET Framework自动创建的,将看起来不同于你的电脑。但您可以看到USERID是您当前用户的Windows用户ID,FIRMNAME是您指定的程序集信息的一部分,路径中也使用了程序集名称和版本。

You can't exactly tell the path as it is created automatically by the .NET Framework, and it will look different on your PC. But you can see that USERID is the Windows user ID of your current user, FIRMNAME is part of the assembly information you have specified, and the assembly name and version is also used in the path.

注意


  • 如果您使用设计设计器(项目中的设置标签),它将创建一个名为 Settings.Settings c $ c> Settings.Designer.cs 以通过C#代码访问帖子)。这是设置的副本,因为它将存储在您的 Web.config App.config 文件(取决于您的项目类型,仅适用于应用程序范围设置 - 基于用户配置文件存储用户范围设置)。您可以创建其他 *。settings 文件并使用它们(如 here )。

  • If you're using the Settings Designer (Settings tab in your project), it will create a file named Settings.Settings (along with Settings.Designer.cs to access the sessings via C# code) in the Properties section of your project. This is a copy of the settings as it will be stored in your Web.config or App.config file as well (depending on your project type, only for application scope settings - user scope settings are stored based on the user profile). You can create additional *.settings files and use them (as it is described here).

用户设置,此处。有关存储用户设置的位置的详细信息,请查看

There is an upgrade method available for user settings, which is described here. More details about the location where user settings are stored can be found there.

配置中的 < appSettings> 部分有所不同,因为它不区分User scope,它不支持不同的数据类型,只是字符串。然而,可以容易地读取和写入配置键/值。
如果你对代码感兴趣,你可以在这里找到它(在stackoverflow):

如何读取/写入appSettings的配置设置

The <appSettings> section in the configuration works differently, since it does not distinguish "User" and "Application" scope and it does not support different datatypes, just strings. However, it is possible to easily read and write the configuration keys/values. If you're interested in the code, you can find it here (on stackoverflow):
how to read/write config settings of appSettings

这篇关于如何从加载的App.config文件中检索ApplicationSettings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆