Last post Apr 13, 2007 06:57 AM by kaanoezturk
Apr 13, 2007 02:38 AM|kaanoezturk|LINK
I have to modify the web.config using an exe. The WebConfigurationManager accepts only virtual directories as parameter. How can I acces the the web.config file physically?
Apr 13, 2007 04:51 AM|tomaszwaraksa|LINK
Try ConfigurationManager class. For more details, press F1 in Visual Studio and go to the following help page:
Apr 13, 2007 05:36 AM|kaanoezturk|LINK
Apr 13, 2007 05:48 AM|AshokRaja|LINK
This article explains how to edit webcofig file by physically locating the file. Make some changes if required on the source code.
Hope this will help you
Apr 13, 2007 06:16 AM|tomaszwaraksa|LINK
Ahh, silly me. After another coffee I now start thinking clear.
You still should use the WebConfigurationManager, as it possesses all the knowledge of the web.config object model. But, as you've noticed, WebConfigurationManager.OpenWebConfiguration method accepts only virtual paths and relies on IIS
metabase to resolve the actual physical path of the configuration file. Fortunately, another method,
OpenMappedWebConfiguration comes to the rescue. Using this one you can create a custom mapping from a virtual path to the physical path of your choice, and let the WebConfigurationManager use your mapping instead of asking the IIS:
Try this code, I tested it myself, it works:
string dummyVirtualPath = "/MyApp";
string physicalPath = @"c:\Software\FleetConnect\Web";
WebConfigurationFileMap map = new WebConfigurationFileMap();
map.VirtualDirectories.Add(dummyVirtualPath, new VirtualDirectoryMapping(physicalPath, true));
Configuration configuration = WebConfigurationManager.OpenMappedWebConfiguration(map, dummyVirtualPath);
foreach (KeyValueConfigurationElement element in configuration.AppSettings.Settings)
listBox1.Items.Add(String.Concat(element.Key, "=", element.Value));
Apr 13, 2007 06:57 AM|kaanoezturk|LINK
Thank you so much! This really helped me.
I've had two cups of coffee and 0,4l coke, but not any good solution yet [:)]