How can I change the Custom error mode in web.config file programmatically? I need to change the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections />
...
<system.web>
....
<customErrors mode="RemoteOnly">
<error statusCode="404" redirect="~/errors/404" />
</customErrors>
To
<customErrors mode="off">
<error statusCode="404" redirect="~/errors/404" />
</customErrors>
Does any one have any idea about this?
UPDATE
As CoDe aDDict answer, I try to use this:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
CustomErrorsSection CustomErrorsection = (CustomErrorsSection)config.GetSection("system.web/customErrors");
CustomErrorsection.Mode = CustomErrorsMode.Off;
config.Save();
But there is an exception:
Access to the path 'D:\Projects\MyProject\web.config' is denied.