通过ConfigurationManager访问用户设置 [英] Access User Settings via ConfigurationManager

查看:62
本文介绍了通过ConfigurationManager访问用户设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想从本地用户设置文件中读取设置.我正在从库中进行设置(与设置所属的应用程序相对).这是我的App.Config压缩文件:

Hi guys,

I want to read a setting from the local user settings file. I''m doing it from a library (as opposed to the application whom the settings belong to). This is my cut-down App.Config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="XXX.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <XXX.Properties.Settings>
            <setting name="Language" serializeAs="String">
                <value>English (United Kingdom)</value>
            </setting>
        </XXX.Properties.Settings>
    </userSettings>
</configuration>



我可以创建一个Configuration类:



I can create a Configuration class:

System.Configuration.Configuration userConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);



但是我不确定访问语言"设置值的最佳方法.我在网上搜索失败,但是我感觉可能不得不创建一个自定义类.我过去曾创建过自定义配置类,但我想知道是否有一种更快的获取用户设置的方法.



but I''m not sure on the best approach to access the value of the Language setting. I have scoured the web unsuccessfully but I have a feeling I may have to create a custom class. I have created custom configuration classes in the past but I wonder if there''s a quicker way of getting user settings.

推荐答案

不是吗Settings.Default.[SettingNameHere]?

抱歉,我有点不小心阅读了这个问题.

如果另一个程序集是项目的类库类型,则应在配置节(自定义项)中显示将运行"的项目.我不确定dll/class库项目是否能够从将运行的项目中访问用户设置.您可以尝试一下.

如果无法访问它,则可以使用LINQ/XPath读取与常规XML文件相同的配置.
Isn''t it Settings.Default.[SettingNameHere]?

Sorry, I was a bit careless reading the question.

If the other assembly is a class library type of project, then the configuration sections (custom ones) should be present the projects which would "run". I am not sure if the dll/class library project will be able to access the user settings as well from the project which will run. You can try that out.

If it is not able to access it, you can use LINQ/XPath to read the configuration same as a normal XML file.


这篇关于通过ConfigurationManager访问用户设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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