gpt4 book ai didi

c# - 在设置中构建时无法访问 App.config

转载 作者:行者123 更新时间:2023-11-30 20:34:35 26 4
gpt4 key购买 nike

我有一个非常简单的应用程序,我正在尝试使用 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

有什么线索吗?

最佳答案

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

当使用内置设置机制时,Visual Studio 已经为您创建并维护了 app.config 文件和一个 Settings 类。当您构建解决方案时,app.config 文件将被重命名并作为 applicationname.exe.config 复制到输出目录。

您需要做的就是通过自动生成的属性访问您的设置。这适用于应用程序和用户设置。在你的例子中:

string s = Properties.Settings.Default.Hospkey;

完成!仅此而已!

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

Properties.Settings.Default.UserSettingName = value;
Properties.Settings.Default.Save();

完成!仅此而已!

请停止使用配置管理器或任何东西,也不要在输出中包含 app.config 文件。默认情况下都在那里!

用户设置是指用户应该能够通过应用程序的 GUI(例如设置对话框)更改的值,或者将根据应用程序的状态(例如持久窗口位置、MRU文件等),并将保存在用户配置文件中的某个地方(不管在哪里 - 只需将其视为给定的)并且也会自动从那里读取。

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

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

关于c# - 在设置中构建时无法访问 App.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39020372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com