gpt4 book ai didi

c# - 在 DAL 层使用 appsettings

转载 作者:太空狗 更新时间:2023-10-29 22:24:59 24 4
gpt4 key购买 nike

我有一个 winforms 应用程序,其中一些数据存储在 XML 文件中。这些 XML 文件的存储位置可以由用户配置,并存储在 AppSettings 中。我所有的层都是单独的组件。我可以从我的 DAL 程序集访问我的设置,还是应该将其作为参数传递到我的所有层?

当我尝试从我的 DAL 层读取设置时,我遇到了另一个问题

        Configuration config = ConfigurationManager.OpenExeConfiguration(
System.Reflection.Assembly.GetEntryAssembly().Location);
string dataStorageLocation = config.AppSettings["DataStorageLocation"];

config.AppSettings["DataStorageLocation"] 给出编译错误:System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty] 由于其保护级别而无法访问。这是为什么?

有人能让我走上正轨吗?谢谢。

最佳答案

您需要使用 config.AppSettings.Settings["DataStorageLocation"]。参见 the MSDN documentation获取样本。

或者,恕我直言,您可以使用 System.Configuration.ConfigurationManager.AppSettings[name] 访问主机应用程序的 AppSettings。这可能比您的技术更灵活,因为如果您的 DAL 程序集托管在 IIS 的服务层中,它也可以工作。以这种方式直接从主机应用程序的配置文件访问配置信息是完全可以接受的,并且通常比通过层向下传递配置信息要好。

关于c# - 在 DAL 层使用 appsettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1995012/

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