开发者

Reading settings from App.Config

Config file.

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.

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 n开发者_如何学运维ame="username" serializeAs="String">
                <value>MyUserName</value>
            </setting>
            <setting name="password" serializeAs="String">
                <value>MyPassword</value>
            </setting>
        </Fulfillment.Properties.Settings>
    </applicationSettings>
</configuration>

Please help. Thanks.


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

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

You can read them as follows:

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

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).


I use User variables in my application, and access them with:

var PropertyValue = Properties.Settings.Default.PropertyName

where PropertyName would be MyUserName or MyPassword in your case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜