访问调用应用程序的设置(app.config)

时间:2022-12-31 14:12:02

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

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

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有一个Settings.settings(Visual Studio Designer)和app.config组合。我收集的是设计器是自动生成app.config文件的前端。要在WF中使用这些设置,我使用它自动生成的类的强类型属性(即WF.Settings.MyTimeOutSetting)。

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?

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

4 个解决方案

#1


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.

就像约翰所说,这是一个坏主意。调用者(在这种情况下为exe)应该将所需的信息传递给DLL。这样你就可以在以后的某个地方重新使用DLL,并且对app.config设置没有一些“隐形”依赖。

Try this:

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

#2


The answer is: don't do it.

答案是:不要这样做。

Have the calling application either pass you whatever you need to know in the call, or perhaps in your constructor. The called component should not require knowledge of the caller.

让调用应用程序在调用中或者在构造函数中将您需要知道的任何内容传递给您。被调用的组件不应该需要调用者的知识。

#3


Add a reference to System.Configuration to your proejct.

在您的项目中添加对System.Configuration的引用。

Then in the particular .cs or .vb (or whatever) file you are wishing to make a reference to the config file add the following:

然后在特定的.cs或.vb(或其他)文件中,您希望引用配置文件,添加以下内容:

C#: using System.Configuration; VB: Imports System.Configuration

C#:using System.Configuration; VB:Imports System.Configuration

then you can access the web config by using something like this:

然后您可以使用以下内容访问Web配置:

C#: System.Configuration.COnfigurationManager.AppSettings["THE_SETTING_U_WANT"] ;

C#:System.Configuration.COnfigurationManager.AppSettings [“THE_SETTING_U_WANT”];

VB: System.Configuration.COnfigurationManager.AppSettings("THE_SETTING_U_WANT")

IF you want a full section I think there are methods in that class to do that as well.

如果你想要一个完整的部分,我认为该类中也有方法可以做到这一点。

#4


The same way you would in the windows forms. It will atomically look in the app.config of the calling app first

与Windows窗体中的方式相同。它将首先以原子方式查看调用应用程序的app.config

#1


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.

就像约翰所说,这是一个坏主意。调用者(在这种情况下为exe)应该将所需的信息传递给DLL。这样你就可以在以后的某个地方重新使用DLL,并且对app.config设置没有一些“隐形”依赖。

Try this:

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

#2


The answer is: don't do it.

答案是:不要这样做。

Have the calling application either pass you whatever you need to know in the call, or perhaps in your constructor. The called component should not require knowledge of the caller.

让调用应用程序在调用中或者在构造函数中将您需要知道的任何内容传递给您。被调用的组件不应该需要调用者的知识。

#3


Add a reference to System.Configuration to your proejct.

在您的项目中添加对System.Configuration的引用。

Then in the particular .cs or .vb (or whatever) file you are wishing to make a reference to the config file add the following:

然后在特定的.cs或.vb(或其他)文件中,您希望引用配置文件,添加以下内容:

C#: using System.Configuration; VB: Imports System.Configuration

C#:using System.Configuration; VB:Imports System.Configuration

then you can access the web config by using something like this:

然后您可以使用以下内容访问Web配置:

C#: System.Configuration.COnfigurationManager.AppSettings["THE_SETTING_U_WANT"] ;

C#:System.Configuration.COnfigurationManager.AppSettings [“THE_SETTING_U_WANT”];

VB: System.Configuration.COnfigurationManager.AppSettings("THE_SETTING_U_WANT")

IF you want a full section I think there are methods in that class to do that as well.

如果你想要一个完整的部分,我认为该类中也有方法可以做到这一点。

#4


The same way you would in the windows forms. It will atomically look in the app.config of the calling app first

与Windows窗体中的方式相同。它将首先以原子方式查看调用应用程序的app.config