如何取回从加载app.config文件的applicationSettings [英] How do I retrieve ApplicationSettings from a loaded app.config file

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

问题描述

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

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

我已经找到了一个例子<一href="http://stackoverflow.com/questions/1682054/how-do-i-retrieve-appsettings-from-the-assembly-config-file">How做我找回的appSettings ,但我不能找出如何访问的的applicationSettings 这样的。

I have found an example How do I retrieve appSettings, but i can't find out how to access applicationSettings this way.

推荐答案

的applicationSettings 只读运行时。您可以设置/直接修改它们或者通过在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:

  • 如果设置应用到整个应用程序(为所有用户),请选择应用程序的适用范围。
  • 如果每个用户都应该有不同的设置(绑定到用户配置文件),然后选择用户

例如,如果你创建 myOwnSetting 在项目中的 WindowsFormsTestApplication1 如下:

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#code自动访问该设置(这就是为什么你应该这样做在项目属性,而不是通过文本编辑器) - 保存了更改后,您可以在应用程序读取其值很容易通过以下code:

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 用户的范围,然后它被存储在一个名为节&LT; userSettings&GT; 而不是&LT;的applicationSettings&GT; ,但你仍然可以用上面的code线访问它。

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.

范围用户的另一个区别设置是你有读写访问,也就是说,它允许执行以下操作:

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();

如果您尝试用相同的应用程序范围设置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已更改为值别的东西 - 但旧的值仍然是在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

在名为 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自动创建的,它会看起来不同的个人电脑上。但是你可以看到,用户标识是当前用户的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.

注意(交叉引用了类似的主题): 与的appSettings 部分是不同的,因为它没有区分用户和应用程序范围,它不支持不同数据类型,只是字符串。然而,这是可以容易地的读写的配置的键/值。 如果你有兴趣在code,你可以在这里找到它(在计算器):

Note (cross-reference to a similar topic): The appSettings section is different, 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):

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

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

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