简体   繁体   English

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

[英]Reference Web.Config file from another project in same solution C#

I have a VC2010 C# Solution, with a number of projects in it. 我有一个VC2010 C#解决方案,里面有很多项目。

So for example, I have a web project, and I have a class library. 例如,我有一个Web项目,我有一个类库。

In the web.config file, I have a key in the <appSettings> section, eg 在web.config文件中,我在<appSettings>部分有一个键,例如

<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. 我还在Web.Production.config文件中添加了一个键,以反映服务器上的文件目录。

So when I reference it in my web project (It's MVC) - I do so like this: 所以当我在我的web项目中引用它时(它是MVC) - 我这样做:

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

this works fine within my web project. 这在我的web项目中工作正常。 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, eg a class library, in the same solution?? 但是,我还需要在类库中引用它,这就是我的问题 - 有没有办法在同一个解决方案中从另一个项目(例如类库)引用web.config文件中的键?

All help is appreciated. 所有帮助表示赞赏。 Thanks 谢谢

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. .Net将在启动应用程序域的应用程序的配置文件中查找配置密钥。 A class library used by such an application will have access to it's config file. 这样的应用程序使用的类库将可以访问它的配置文件。

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.) 这并不总是方便(例如,如果您为MMC管理单元编写基于.net的插件,则这意味着您必须修改系统文件夹中的mmc.exe.config。)

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. 这个问题有关于与dll配置相关的陷阱的更多细节,但是如果需要,还有一些技术可以做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#我的测试项目无法从另一个项目的web.config / app.config文件正确加载数据 - C# My test project does not load correctly data from the web.config / app.config file of another project 从同一解决方案C#中的另一个项目中读取配置属性 - Read config properties from another project in the same solution C# 从另一个项目访问Web.config - Accessing Web.config from another Project 从C#中的外部dll访问web.config文件 - Accessing web.config file from an external dll in C# 我如何获取在Web项目中生成的pdf文件,并在同一解决方案c#中将其打印在另一个项目中 - How could I get a pdf file generated in a web project, and print it in another project in the same solution c# 如何从C#中另一个应用程序的app.config / web.config文件中提取连接字符串 - How to extract connection string from another application's app.config / web.config file in C# 在同一解决方案中将父项目的Web配置引用到子项目 - Reference web config of parent project to child project in same Solution 使用C#替换另一台计算机(共享文件夹/网络)上存在的文件(web.config)中的值 - Replace value in file(web.config) present on another machine(shared folder/network) using C# 在同一解决方案中使用另一个项目的引用 - Use reference from another project at the same solution 从相同解决方案C#的另一个项目访问资源文件 - Accessing Resource files from another project of same solution C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM