访问调用应用程序的设置(app.config) [英] Accessing the settings (app.config) of the calling application

查看:135
本文介绍了访问调用应用程序的设置(app.config)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms应用程序("WF"),以及一个由该WinForms应用程序("LIB")调用的库

I have a WinForms application ("WF"), and a Library called by that WinForms application ("LIB")

WF具有Settings.settings(Visual Studio设计器)和app.config组合.我收集到的信息比设计器是一个自动生成app.config文件的前端.要在WF中使用这些设置,我使用它自动生成的类的强类型属性(即WF.Settings.MyTimeOutSetting).

WF has a Settings.settings (Visual Studio Designer) and app.config combo. I gather than the designer is a front end that auto generates the app.config file. To use those settings from within WF, I use the strongly typed properties of the class it autogenerates (i.e. WF.Settings.MyTimeOutSetting).

当WF在LIB中调用方法时,我想从lib中使用WF的设置之一.在被调用方(LIB)代码中,如何从调用方(WF)的app.config中检索设置?

When WF calls a method in LIB, I want to use one of WF's settings from within lib. How can I retrieve a setting from the caller's (WF's) app.config while in the callee's (LIB's) code?

推荐答案

就像约翰所说,这是一个坏主意.调用方(在这种情况下为exe)应将所需的信息传递给DLL.这样一来,您以后就可以在其他地方重用DLL,而不必对app.config设置有不可见"的依赖.

Like John said, this is a bad idea. The caller (exe in this case) should pass the needed information to the DLL. That way you can re-use the DLL later, somewhere else, and not have some 'invisible' dependency on an app.config setting.

尝试一下:

Dim oConfiguration As System.Configuration.Configuration
oConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim sValue As String = oConfiguration.AppSettings.Settings("setting").Value

这篇关于访问调用应用程序的设置(app.config)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆