从app.config中读取设置app、config

2023-09-04 23:34:50 作者:Ⅱ←伤我者的碑

配置文件。

我应该如何阅读的用户名和密码的使用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).