Teena
Joined: 28 Aug 2007 Posts: 143
|
Posted: Mon Mar 01, 2010 10:25 am Post subject: Dynamically change AppSettings values in ASP.Net using C#? |
|
|
Hi All,
Is it possible to change AppSettings values of my application? If, provide me a code snippet to do changes in my web.config.
Following is my web.config file.
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="SetupRan" value="true"/>
<add key="WebServer" value="IIS"></add>
</appSettings>
<system.web>
<!-- DYNAMIC DEBUG COMPILATION
Set compilation debug="true" to enable ASPX debugging. Otherwise, setting this value to
false will improve runtime performance of this application.
Set compilation debug="true" to insert debugging symbols (.pdb information)
into the compiled page. Because this creates a larger file that executes
more slowly, you should set this value to true only when debugging and to
false at all other times. For more information, refer to the documentation about
debugging ASP.NET files.
-->
<compilation defaultLanguage="c#" debug="true">
<compilers>
<compiler language="c#" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" extension=".cs" compilerOptions="/d:DEBUG;TRACE"/></compilers>
<assemblies>
<add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
<!-- AUTHENTICATION
This section sets the authentication policies of the application. Possible modes are "Windows",
"Forms", "Passport" and "None"
-->
<authentication mode="Windows"/>
<!-- AUTHORIZATION
This section sets the authorization policies of the application. You can allow or deny access
to application resources by user or role. Wildcards: "*" mean everyone, "?" means anonymous
(unauthenticated) users.
-->
<identity impersonate="true"/>
<authorization>
<deny users="?"/>
<!-- Allow all users -->
<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>
<!-- CUSTOM ERROR MESSAGES
Set mode="on" or "remoteonly" to enable custom error messages, "off" to disable. Add
<error> tags for each of the errors you want to handle.
-->
<customErrors mode="RemoteOnly" defaultRedirect="error.aspx">
</customErrors>
<xhtmlConformance mode="Legacy"/></system.web>
</configuration>
In my above file, I want to change values of setran in Appsettings section.
Thanks for your help in Advance...
Bye..... |
|
Raghavendra
Joined: 21 Aug 2007 Posts: 222
|
Posted: Mon Mar 01, 2010 10:28 am Post subject: |
|
|
Hi Teena,
In ASP.Net, WebConfigurationManager provides the access to change web applications configuration files. Following is the sample code.
Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection appSettings = (AppSettingsSection)configuration.GetSection("appSettings");
if (appSettings != null)
{
appSettings.Settings["SetupRan"].Value = "true";
configuration.Save();
}
That's it.
Thanks & Regards,
Raghavendra K. |
|