引用同一解决方案 C# 中另一个项目的 Web.Config 文件

发布于 2024-10-30 13:55:47 字数 539 浏览 3 评论 0原文

我有一个 VC2010 C# 解决方案,其中有许多项目。

例如,我有一个网络项目,并且有一个类库。

在 web.config 文件中,我在 部分中有一个键,例如,

<add key="FileDirectory" value="G:\ftproot\sales" />

我还在 Web.Production.config 文件中添加了一个键以反映服务器上的文件目录。

因此,当我在我的 Web 项目(它是 MVC)中引用它时 - 我这样做:

var FTPPath = ConfigurationManager.AppSettings["FileDirectory"];

这在我的 Web 项目中运行良好。但是,我还需要在类库中引用它,这引起了我的问题 - 有没有办法在同一解决方案中从另一个项目(例如类库)引用 web.config 文件中的键?

感谢所有帮助。 谢谢

I have a VC2010 C# Solution, with a number of projects in it.

So for example, I have a web project, and I have a class library.

In the web.config file, I have a key in the <appSettings> section, e.g.

<add key="FileDirectory" value="G:\ftproot\sales" />

I have also added a key in the Web.Production.config file to reflect the file directory on the server.

So when I reference it in my web project (It's MVC) - I do so like this:

var FTPPath = ConfigurationManager.AppSettings["FileDirectory"];

this works fine within my web project. However, I also need to reference this in the class library, which gets to my question - Is there a way to reference a key in the web.config file from another project, e.g. a class library, in the same solution??

All help is appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2024-11-06 13:55:48

是的,您可以使用完全相同的代码。 .Net 将在启动应用程序域的应用程序的配置文件中查找配置密钥。此类应用程序使用的类库将有权访问其配置文件。

Yes you can use exactly the same code. .Net will look up the configuration key in the config file of the application which started the app domain. A class library used by such an application will have access to it's config file.

〗斷ホ乔殘χμё〖 2024-11-06 13:55:48

类库没有自己的配置。它们使用正在使用的可执行文件的配置。

这意味着对于您来说,您应该能够使用相同的代码,并且它将从配置中读取设置(假设它在那里)。

但这并不总是很方便(例如,如果您为 MMC 管理单元编写基于 .net 的插件,因为这意味着您必须修改系统文件夹中的 mmc.exe.config。)

您可能最好有一个方法将此所需的配置设置传递到您的库代码中。然后,在您控制配置的应用程序中,您可以从那里读取配置并将其传递进去,而在您无法控制配置的应用程序中,您可以使用另一种方法,例如从注册表或手动读取配置文件中读取。或者两全其美,这样您就可以将其传递进去,如果不这样做,它会尝试从默认配置中读取它。

这个问题有一些关于与 dll 配置相关的陷阱的更多细节,但也有一些技术如果需要的话就去做。

class libraries do not have their own configuration. They use the configuration of which ever executable they are being used in.

This means that for you you should be able to use the same code, and it will read the setting from the config (assuming that it is there).

This is not always convenient though (for example if you write a .net based plugin for a MMC snap-in, as this means you have to modify the mmc.exe.config in the system folder.)

You might be better having a method to pass this required configuration setting into you library code. then in apps where you control the config you can just read it from there and pass it in, and in apps where you can't you can use another approach, like reading from the registry or from a manually read config file. Or have the best of both worlds and make it so you can pass it in, and if this is not done it attempts to read it from the default configuration.

This question has some more details on the pitfalls associated with dll configuration, but also has some techniques for doing it if you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文