在DAL层中使用appsettings

Pet*_*ter 5 c# appsettings winforms

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

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

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

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

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

Joe*_*Joe 8

你需要使用config.AppSettings.Settings["DataStorageLocation"].有关示例,请参阅MSDN文档.

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