I have a win form that creates a site in IIS7. One function needs to open the web.config file and make a few updates. (connection string, smtp, impersonation)
However I do not have the virtual path, just the physical path.
Is there any way I can still use WebConfigurationManager?
I need to use it's ability to find section and read/write.
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
Building on Vadim's answer, I found what he wrote didn't exactly work for my situation, so I used this instead:
In case anyone else is trying what I'm trying and finds this, the purpose of my doing this was to get
SimpleMembershipProvider
, which inherits fromExtendedMembershipProvider
, to work with SQLite. To do that, I created the tables manually per this link: SimpleMembershipProvider in MVC4, and then used this command in myGlobal.asax
file'sApplication_Start
routine:Which it turns out didn't require me to actually re-write my web.config file at all. (There were also a lot of web.config changes I had to do, but that is even more out of the scope of this question.)
Vadim's answer worked great on our dev server, but bombed out on our live server with the following message:
To correct this, I found another overload for
WebConfigurationManager.OpenMappedWebConfiguration
that takes the IIS website name as the third parameter. The result is as follows:Vadim's answer was exactly what I needed, but I came across the same issue as Kieth, and his solution did the trick!
I thought I'd add though, that the IIS Website name can be retrieved by calling:
Also, cjbarth's code included a tidy solution for those testing in environments where the location of wwwroot and Web.config can vary:
So with these in mind another slight improvement on Vadim's function would read:
You will have to map the physicalPath to a virtualPath. Here is how you would do that.