从app.config中读取设置 [英] Reading settings from App.Config

查看:161
本文介绍了从app.config中读取设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

配置文件。

我应该如何阅读的用户名和密码的使用System.Configuration.ConfigurationManager类在C#中值?我尝试了好几种东西,但都无济于事。

How should I read values of username and password in C# using System.Configuration.ConfigurationManager class? I have tried several stuff but to no avail.

的的app.config如下。

The app.config is given below.

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Fulfillment.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Fulfillment.Properties.Settings>
            <setting name="username" serializeAs="String">
                <value>MyUserName</value>
            </setting>
            <setting name="password" serializeAs="String">
                <value>MyPassword</value>
            </setting>
        </Fulfillment.Properties.Settings>
    </applicationSettings>
</configuration>

请帮忙。谢谢你。

推荐答案

如果该值在文件的appSettings部分:

If the values are in the appSettings section of the file:

<appSettings>
  <add key="myUsername" value="david"/>
  <add key="myPassword" value="iLikeHalibut"/>
</appSettings>

您可以阅读它们如下:

string myUsername = System.Configuration.ConfigurationManager.AppSettings["myUsername"];

您将需要确保的程序集,你是程序设计中引用了System.Configuration.dll(它并不总是存在默认情况下)。

You will need to make sure the assembly you are programming contains a reference to System.Configuration.dll (it's not always there by default).

这篇关于从app.config中读取设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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