How can i retrieve a connectionString from a web.config file?

旧城冷巷雨未停 提交于 2020-01-05 03:59:13

问题


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?

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?


回答1:


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"];



回答2:


I would use RMI to query the other application for the ConnectionString.



来源:https://stackoverflow.com/questions/550072/how-can-i-retrieve-a-connectionstring-from-a-web-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!