如何从web.config文件中检索connectionString? [英] How can i retrieve a connectionString from a web.config file?
本文介绍了如何从web.config文件中检索connectionString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用C#编写一个客户端应用程序,应该从我编写的另一个应用程序更改web.config文件中的ConnectionString设置. 我怎样才能实现这个目标?
I am writing an client application in C# which will be supposed to change ConnectionString settings in a web.config file from another application I wrote. How can I achieve this goal?
是否可以在应用程序中加载web.config文件并读取/更改其面向数据对象的方法?还是我需要像解析完整的未知" XML文件一样对其进行解析?
Is there a way to load the web.config file in my application and read/change its data object orientated? Or do I need to parse it as if beeing a complete 'unknown' XML file?
推荐答案
If you're doing this from another application, you can use the VirtualDirectoryMapping class:
VirtualDirectoryMapping vdm = new VirtualDirectoryMapping(@"C:\Inetpub\wwwroot\YourApplication", true);
WebConfigurationFileMap wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
// Get the connectionString
Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
string connection = config.ConnectionStrings.ConnectionStrings["YourConnectionString"];
这篇关于如何从web.config文件中检索connectionString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文