.net 正在从App.Config读取设置

.net 正在从App.Config读取设置,.net,xml,configuration,app-config,configurationmanager,.net,Xml,Configuration,App Config,Configurationmanager,配置文件 如何使用System.Configuration.ConfigurationManager类读取C中的用户名和密码值?我试过几种东西,但都没用 app.config如下所示 <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, Syst

配置文件

如何使用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>

请帮忙。谢谢。

我在应用程序中使用用户变量,并通过以下方式访问它们:

var PropertyValue = Properties.Settings.Default.PropertyName

其中PropertyName在您的情况下是MyUserName或MyPassword。

我在应用程序中使用用户变量,并通过以下方式访问它们:

var PropertyValue = Properties.Settings.Default.PropertyName

其中PropertyName在您的情况下是MyUserName或MyPassword。

如果值在文件的appSettings部分:

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

您需要确保正在编程的程序集包含对System.Configuration.dll的引用。默认情况下,该程序集并不总是在那里。

如果这些值位于文件的appSettings部分:

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

您需要确保正在编程的程序集包含对System.Configuration.dll的引用。默认情况下,它并不总是在那里。

这不是有效的XML文件。我没有看到正在关闭的applicationSettings元素。@Oded,这显然是他的web.config文件的摘录。有多个节点未关闭。他只是发布了他认为相关的内容。@mjw06d-我很久以前就学会了不要做这样的假设。如果他说这是一篇摘录,那很好。这不应该是应用程序设置,而不是应用程序设置吗?或者Microsoft是否同时使用这两个标记?这不是有效的XML文件。我没有看到正在关闭的applicationSettings元素。@Oded,这显然是他的web.config文件的摘录。有多个节点未关闭。他只是发布了他认为相关的内容。@mjw06d-我很久以前就学会了不要做这样的假设。如果他说这是一篇摘录,那很好。这不应该是应用程序设置,而不是应用程序设置吗?或者Microsoft同时使用这两个标记?抱歉,我看到您编辑了您的帖子,并且正在使用应用程序值。好的,谢谢,这很有效。我不知道。但是应该有一种使用ConfigurationManager类读取它们的方法。不是吗?我以前没有使用过ConfigurationManager,但仅供参考,我想指出的另一点是,您可能希望使用userSettings,而不是appSettings。根据我的经验,appSettings只能通过在Visual Studio中更改值并重新编译应用程序来更改。appSettings允许您直接修改.exe.config。对不起,我看到您编辑了您的帖子,并且正在使用应用程序值。好的,谢谢,这很有效。我不知道。但是应该有一种使用ConfigurationManager类读取它们的方法。不是吗?我以前没有使用过ConfigurationManager,但仅供参考,我想指出的另一点是,您可能希望使用userSettings,而不是appSettings。根据我的经验,appSettings只能通过在Visual Studio中更改值并重新编译应用程序来更改。appSettings允许您直接修改.exe.config。是的,我知道,我刚刚遇到了该app.config,并尝试使用ConfigurationManager进行读取,但没有结果。但并没有提到设置类,它可以通过项目的属性类读取。谢谢大家。是的,我知道,我刚刚遇到了app.config,并试图使用ConfigurationManager阅读,但没有效果。但并没有提到设置类,它可以通过项目的属性类读取。谢谢大家。