我无法从App.config

时间:2018-08-09 08:31:27

标签: c# .net wpf visual-studio

我正在尝试从App.config中获取数据,并进行搜索,发现我找到了四种获取数据的方法,但是基本上您应该使用

ConfigurationManager.AppSettings["NameKey/NameSetting"];

我的App.config代码为

   <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="DXThemeManager" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="InterfazPrueba1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <DXThemeManager>
            <setting name="ApplicationThemeName" serializeAs="String">
              <value>Office2016White</value>
            </setting>
        </DXThemeManager>
    </userSettings>
    <applicationSettings>
        <InterfazPrueba1.Properties.Settings>
            <setting name="HostBD" serializeAs="String">
                <value>LOCALHOST</value>
            </setting>
            <setting name="NombreBD" serializeAs="String">
                <value>Pruebas1</value>
            </setting>
            <setting name="ServerBD" serializeAs="String">
                <value>DESKTOP-O037RKH</value>
            </setting>
            <setting name="InstanceBD" serializeAs="String">
                <value />
            </setting>
            <setting name="UsuarioBD" serializeAs="String">
                <value>sa</value>
            </setting>
            <setting name="PassBD" serializeAs="String">
                <value>123</value>
            </setting>
        </InterfazPrueba1.Properties.Settings>
    </applicationSettings>
</configuration>

我班上的代码是:

 private void datosConexion()
    {
        server = ConfigurationManager.AppSettings["ServerBD"];
        user = ConfigurationManager.AppSettings["UsuarioBD"];
        pass = ConfigurationManager.AppSettings["PasswordBD"];
        host = ConfigurationManager.AppSettings["HostBD"];
        db= ConfigurationManager.AppSettings["NombreBD"];
        instance = ConfigurationManager.AppSettings["InstanceBD"];
        Console.WriteLine("Datos:"+server + user + pass + host + db + instance);
    }

所有变量都没有。

我已经尝试了所有方法,并将值更改为键,但是我只能检索数据... 对什么是错或我在做什么不好有任何想法吗?

感谢您能给我的所有支持。

1 个答案:

答案 0 :(得分:3)

ConfigurationManager.AppSettings["NameSetting"]检索App.config中定义的设置,如下所示:

<configuration>
   <configSections>
      ...
   </configSections>
   <userSettings>
      ...
   </userSettings>
    <appSettings>
         <add key="NameSetting" value="123" />
    </appSettings>
</configuration>

如果您在“项目”->“属性”->“设置”下添加项目特定的设置,则可以通过为您生成的类访问这些设置:

string setting = InterfazPrueba1.Properties.Settings.Default.HostBD;