在“设置”中构建时无法访问App.config

时间:2016-08-18 14:07:29

标签: c# app-config application-settings configurationmanager appsettings

我有一个非常简单的应用程序,我正在尝试使用Settings属性构建app.config。 app.config是构建的,但是在运行时,键不会检索值。

我最初使用从项目添加应用程序配置文件并手工构建app.config并取得了巨大成功手工构建的app.config如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="Hosppath" value="C:\events\inbound"/>
    <add key="Vendpath" value="C:\events\outbound"/>
    <add key="Hospname" value="OBLEN_COB_Active_Inv_Advi_Daily_.csv"/>
    <add key="Vendname" value="Advi_OBlen_Active_Inv_Ack_Daily_.csv"/>
    <add key="Hospkey" value="invoice_number"/>
    <add key="Vendkey" value="invoice"/>
    <add key="Outpath" value="C:\events\Reports"/>
    <add key="Outname" value="OB_Exception_Report_.xlsx"/>
    <add key="Hospexcpt" value="O'Bleness Exceptions"/>
    <add key="Vendexcpt" value="AdviCare Exceptions"/>
  </appSettings>
</configuration>

调用类在这里:

using System;
using System.Configuration;

namespace LoadSettings
{
    public static class ConfigReader
    {
        public static bool ReadAllSettings()
        {
            var success = false;
            var appSettings = ConfigurationManager.AppSettings;

            if(appSettings.Count > 0)
            {
                success = true;
            }
            return success;
        }

        public static string ReadSetting(string key)
        {
            string result = "";

            var appSettings = ConfigurationManager.AppSettings;
            result = appSettings[key] ?? "Not Found";

            return result;
        }
    }
}

Main()函数如下所示:

using System;
using System.Configuration;

namespace LoadSettings
{
    class Program
    {
        static void Main(string[] args)
        {
            string hospPath;
            string vendPath;
            string hospName;
            string vendName;
            string hospKey;
            string vendKey;
            string rptPath;
            string rptName;
            string sht1Name;
            string sht2Name;

            if (ConfigReader.ReadAllSettings())
            {
                hospPath = ConfigReader.ReadSetting("Hosppath");
                vendPath = ConfigReader.ReadSetting("Vendpath");
                hospName = ConfigReader.ReadSetting("Hospname");
                vendName = ConfigReader.ReadSetting("Vendname");
                hospKey = ConfigReader.ReadSetting("Hospkey");
                vendKey = ConfigReader.ReadSetting("Vendkey");
                rptPath = ConfigReader.ReadSetting("Outpath");
                rptName = ConfigReader.ReadSetting("Outname");
                sht1Name = ConfigReader.ReadSetting("Hospexcpt");
                sht2Name = ConfigReader.ReadSetting("Vendexcpt");

                Console.WriteLine("The hospital file path is:      {0}", hospPath);
                Console.WriteLine("The vendor file path is:        {0}", vendPath);
                Console.WriteLine("The hospital file name is:      {0}", hospName);
                Console.WriteLine("The vendor file name is:        {0}", vendName);
                Console.WriteLine("The hospital key field is:      {0}", hospKey);
                Console.WriteLine("The vendor key field is:        {0}", vendKey);
                Console.WriteLine("The exceptions report path is:  {0}", rptPath);
                Console.WriteLine("The exceptions report name is:  {0}", rptName);
                Console.WriteLine("The sheet1 tab name is:         {0}", sht1Name);
                Console.WriteLine("The sheet2 tab name is:         {0}", sht2Name);
            }
        }
    }
}

这就像一个冠军,但一旦我尝试在设置中构建它,我什么都没有回来。这是设置:

Settings image

创建的app.config看起来非常正常:

Renamed to proper application_name.exe.config name

在内部,内容与设置中的键和值匹配:

<?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="OHS_RCIS.Properties.Settings" 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="OHS_RCIS.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <OHS_RCIS.Properties.Settings>
            <setting name="Hospkey" serializeAs="String">
                <value>invoice_number</value>
            </setting>
            <setting name="Vendkey" serializeAs="String">
                <value>invoice</value>
            </setting>
            <setting name="Column2" serializeAs="String">
                <value>curr_tot_acct_bal</value>
            </setting>
            <setting name="Column3" serializeAs="String">
                <value>COL_AGNCY_NAME</value>
            </setting>
            <setting name="Column4" serializeAs="String">
                <value>admit_date</value>
            </setting>
            <setting name="Column5" serializeAs="String">
                <value>discharge_date</value>
            </setting>
            <setting name="Column6" serializeAs="String">
                <value>create_date</value>
            </setting>
            <setting name="Column7" serializeAs="String">
                <value>HSP_ACCOUNT_ID</value>
            </setting>
            <setting name="Outpath" serializeAs="String">
                <value>C:\events\Reports</value>
            </setting>
            <setting name="Outname" serializeAs="String">
                <value>COB_OB_Advi_Placement_Exceptions_.xlsx</value>
            </setting>
            <setting name="Hospexcpt" serializeAs="String">
                <value>O\'Bleness Exceptions</value>
            </setting>
            <setting name="Vendexcpt" serializeAs="String">
                <value>AdviCare Exceptions</value>
            </setting>
            <setting name="Tab1Color" serializeAs="String">
                <value>FFFFE1FF</value>
            </setting>
            <setting name="Tab2Color" serializeAs="String">
                <value>FFE4E1FF</value>
            </setting>
            <setting name="ColumnCount" serializeAs="String">
                <value>7</value>
            </setting>
        </OHS_RCIS.Properties.Settings>
    </userSettings>
    <applicationSettings>
        <OHS_RCIS.Properties.Settings>
            <setting name="Hosppath" serializeAs="String">
                <value>C:\events\inbound</value>
            </setting>
            <setting name="Vendpath" serializeAs="String">
                <value>C:\events\outbound</value>
            </setting>
            <setting name="Hospname" serializeAs="String">
                <value>O\'Bleness Exceptions</value>
            </setting>
            <setting name="Vendname" serializeAs="String">
                <value>AdviCare Exceptions</value>
            </setting>
        </OHS_RCIS.Properties.Settings>
    </applicationSettings>
</configuration>

用于访问app.config的代码如下所示,其中一些添加的行试图哄骗结果:

using System;
using System.Configuration;

namespace OHS_RCIS.Utility
{
    public static class ConfigReader
    {
        public static bool ReadAllSettings()
        {
            var success = false;
            ConfigurationManager.GetSection("userSettings");
            ConfigurationManager.RefreshSection("userSettings");
            var appSettings = ConfigurationManager.AppSettings;

            if (appSettings.Count > 0)
            {
                success = true;
            }
            return success;
        }

        public static string ReadSetting(string key)
        {
            string result = "";

            var appSettings = ConfigurationManager.AppSettings;
            result = appSettings[key] ?? "Not Found";

            return result;
        }
    }
}

The calls where it fails in Main()

我在通过“设置”进行操作时显然遗漏了一些东西,但是有适当的引用,

References

可以看到app.config已正确构建,并使用与我的测试代码段相同的通用代码。

Seen through Debugger

任何线索?

1 个答案:

答案 0 :(得分:4)

你错了!请停止做你正在做的事情(除非我完全误解你的要求......)!!

使用内置设置机制时,Visual Studio已经为您创建并维护app.config文件和Settings类。在构建解决方案时,app.config文件将被重命名并复制到输出目录{/ 1}}。

您只需通过自动生成的属性访问您的设置即可。这适用于应用程序和用户设置。在您的示例中:

applicationname.exe.config

完成!没有更多!

如果您想更改用户设置,请执行以下操作:

string s = Properties.Settings.Default.Hospkey;

完成!没有更多!

请停止使用配置管理器或其他任何内容,也不要在输出中包含Properties.Settings.Default.UserSettingName = value; Properties.Settings.Default.Save(); 文件。它默认都在那里!

用户设置适用于用户应该能够通过应用程序的GUI 更改的值(例如设置对话框),或者会根据应用程序的状态进行更改(例如持久化窗口位置,MRU文件等)并将保存在某个用户配置文件中(它并不重要 - 只需将其视为给定)并自动从那里读取。

更新应用程序时,最终可能会使用构建中的默认值。在这种情况下,请关注instructions here您无法通过更改安装文件夹中的.exe.config文件来覆盖用户设置,因为它们只会在首次运行时从那里读取!

应用程序设置是用户无法更改的默认值。如有必要,您可以在.exe.config文件中手动更改这些内容。