简体   繁体   English

从C#中的外部dll访问web.config文件

[英]Accessing web.config file from an external dll in C#

I am doing a product licensing application using MVC and Webservices. 我正在使用MVC和Webservices进行产品许可申请。 Here if a product(win/web application) needs to perform license validation, then it will call the web service I have implemented in my licensing application. 在这里,如果产品(win / web应用程序)需要执行许可证验证,那么它将调用我在许可应用程序中实现的Web服务。 And it works fine. 而且效果很好。 If a product want to register with this licensing application, Some code is needed to be deployed in every client product, such as code to invoke the web service, do encryption and send details to renewal page if license expired. 如果产品要在此许可应用程序中注册,则需要在每个客户端产品中部署一些代码,例如用于调用Web服务,进行加密并在许可证过期时将详细信息发送到续订页面的代码。 To include these changes to every client product, I am planning to create a dll that implements these many logic and deploy to each product. 为了将这些更改包括到每个客户端产品中,我计划创建一个实现了许多逻辑并部署到每个产品的dll。 Here I want to access web.config/app.config of every client application from my dll and add some keys within in tag. 在这里,我想从我的dll访问每个客户端应用程序的web.config / app.config,并在标记中添加一些键。 Is thing possible to access another application web.config file from an external dll that has been deployed to that application? 是否可以从已部署到该应用程序的外部dll访问另一个应用程序web.config文件?

As discussed, below is an example of how to load a web.config and access the app settings section. 如前所述,以下是如何加载web.config和访问“应用程序设置”部分的示例。

For this local example, I've moved a web.config to C:\\Web.config and are using Linq XML. 对于此本地示例,我已将web.config移至C:\\ Web.config并正在使用Linq XML。

 using System.Xml.Linq;

 XDocument xdoc = XDocument.Load("C:\\web.config"); //Path to your config here!

 XElement appSettingsNode = xdoc.Elements().First()
      .Elements().FirstOrDefault(f=> f.Name == "appSettings");

 foreach (XNode node in appSettingsNode.Nodes())
 {
      var currentSetting = node.ToString(); //Here check the contents for setting you need
 }

Hope that helps 希望能有所帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM