Ash*_*yed 1 .net xml configuration configurationmanager app-config
配置文件.
我应该如何使用System.Configuration.ConfigurationManager类在C#中读取用户名和密码的值?我尝试过几种东西,但无济于事.
app.config如下所示.
<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>
Run Code Online (Sandbox Code Playgroud)
请帮忙.谢谢.
如果值位于文件的appSettings部分中:
<appSettings>
<add key="myUsername" value="david"/>
<add key="myPassword" value="iLikeHalibut"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式阅读:
string myUsername = System.Configuration.ConfigurationManager.AppSettings["myUsername"];
Run Code Online (Sandbox Code Playgroud)
您需要确保您编程的程序集包含对System.Configuration.dll的引用(默认情况下并不总是存在).