简体   繁体   English

从app.config文件中读取

[英]reading from app.config file

I am trying to read StartingMonthColumn and CategoryHeadingColumn from the below app.config file using the code 我试图使用代码从下面的app.config文件中读取StartingMonthColumn和CategoryHeadingColumn

ConfigurationSettings.AppSettings["StartingMonthColumn"]

but it is returning null, also ConfigurationSettings.AppSettings.Count returns zero 但它返回null,ConfigurationSettings.AppSettings.Count也返回零

Please help me to read this in my windows application 请帮我在我的Windows应用程序中阅读此内容

<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="CTARepository.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <CTARepository.Properties.Settings>
            <setting name="Setting" serializeAs="String">
                <value />
            </setting>
        </CTARepository.Properties.Settings>
    </userSettings>
    <appSettings>
        <add key="StartingMonthColumn" value="7"/>
        <add key="CategoryHeadingColumn" value="1"/>
    </appSettings>
</configuration>

Configuration Settings .AppSettings is obsolete , you should use Configuration Manager .AppSettings instead (you will need to add a reference to System.Configuration ) 配置设置 .AppSettings过时 ,您应该使用Configuration Manager .AppSettings (您需要添加对System.Configuration的引用)

int value = Int32.Parse(ConfigurationManager.AppSettings["StartingMonthColumn"]);

If you still have problems reading in your app settings then check that your app.config file is named correctly. 如果在应用程序设置中仍然有问题,请检查您的app.config文件是否正确命名。 Specifically, it should be named according to the executing assembly ie MyApp.exe.config , and should reside in the same directory as MyApp.exe . 具体来说,它应该根据执行程序集命名,即MyApp.exe.config ,并且应该与MyApp.exe位于同一目录中。

Just for the future reference, you just need to add System.Configuration to your references library: 仅供将来参考,您只需将System.Configuration添加到参考库:

在此输入图像描述

ConfigurationSettings.AppSettings is deprecated , see here: 不推荐使用 ConfigurationSettings.AppSettings,请参见此处:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationsettings.appsettings.aspx http://msdn.microsoft.com/en-us/library/system.configuration.configurationsettings.appsettings.aspx

That said, it should still work. 那说,它应该仍然有效。

Just a suggestion, but have you confirmed that your application configuration is the one your executable is using? 只是一个建议,但您确认您的应用程序配置是您的可执行文件正在使用的配置吗?

Try attaching a debugger and checking the following value: 尝试附加调试器并检查以下值:

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

And then opening the configuration file and verifying the section is there as you expected. 然后打开配置文件并验证该部分是否符合您的预期。

Try: 尝试:

string value = ConfigurationManager.AppSettings[key];

For more details check: Reading Keys from App.Config 有关更多详细信息,请检查: 从App.Config中读取密钥

The reason is simple, your call to ConfigurationSettings.AppSettings is not returning the required config file. 原因很简单,您对ConfigurationSettings.AppSettings的调用未返回所需的配置文件。 Please try any of the following ways: 请尝试以下任何一种方式:

  • Make sure your app config has the same name as your application's exe file - with the extension .config appended eg MyApp.exe.config 确保您的应用程序配置与应用程序的exe文件具有相同的名称 - 附加扩展名.config,例如MyApp.exe.config
  • OR you can use ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings["StartingMonthColumn"] 或者您可以使用ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings["StartingMonthColumn"]

Hope this helps 希望这可以帮助

This: 这个:

Console.WriteLine( "StartingMonthColumn is {0}", ConfigurationManager.AppSettings["StartingMonthColumn"]);

works fine for me. 对我来说很好。

Note that ConfigurationManager is in the System.Configuration namespace (so you'll likely want a using System.Configuration; statement), and that since what you read in has a string type you'll need to parse what you read in to use it as a number. 请注意, ConfigurationManager位于System.Configuration命名空间中(因此您可能需要using System.Configuration;语句),并且由于您所读取的内容具有字符串类型,因此您需要解析您读入的内容以使用它作为一个数字。

Also, be sure you set system.configuration.dll as a reference in your project or build script. 此外,请确保将system.configuration.dll设置为项目或构建脚本中的引用。

尝试重建项目 - 它将App.config的内容复制到构建库中的“<YourProjectName.exe> .config”。

还要在运行应用程序的App.config中添加键“StartingMonthColumn”,例如在测试项目的App.config中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM