Exception of type 'System.Web.HttpUnhandledException' was thrown. while modifying web.config file

2.7k Views Asked by At

I want to change web.config file appsetings values which is located in the server. i have given full permission to that folder still unable to access and edit the web.config file

while executing the code its giving error saying - Exception of type 'System.Web.HttpUnhandledException' .unable to access file resides in server 

error:

System.UnauthorizedAccessException: Access to the path
'C:\Hosting\chek\demo.checks.in\wwwroot\web.config' is denied.

aspx.cs

string emailid = txtEmailid.Text.Trim();
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
config.AppSettings.Settings.Remove("kumarSunlightitMailid");
config.AppSettings.Settings.Add("kumarSunMailid",emailid);
config.Save();

lblNewEmailid.Text = ConfigurationManager.AppSettings["kumarSunMailid"] + " is ur new mail id";

web.config

<appSettings>
  <add key="kumarSunMailid" value="[email protected]" />
 </appSettings>
3

There are 3 best solutions below

0
On

You are facing this error because you don't give read/write permissions to user..

check couple of things

  1. check wether web.config is readonly.. if so then uncheck that Option
  2. Right Click on Web.config -> goto properties -> security and check you User does have read/write permissions. if not then give it read/write permissions
5
On

If you haven't tried this, try adding the Everyone User to that folder, and give him full access. Next, could try setting the configSource of appsettings so that these are stored in a separate config file:

<appSettings configSource="appSettings.config" />

And then create a new file in the same folder called appSettings.config:

<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
   <add key="yourKey" value="yourValue" />
</appSettings>

Try adding the Everyone user with full access to both files too.

Otherwise, I'd create a new solution an see if you can recreate this odd behavior there.

0
On

Please give permission to the current user to full access the iis project or solution.