Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Dear All,
 
Hi, I want add some comments in my web.config file at runtime.
Means for example
 
original Web.config file is:
 
<pre lang="xml"><?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
    <appSettings>
  <add key="customsetting1" value="Some text here" />
  <add key="myKey" value=",1,1,1,1,1,1" />
  <add key="RemoveTCPError" value="1,1" />
 </appSettings>
    <connectionStrings>
  <add name="myDatabaseName" connectionString="whatever" />
 </connectionStrings>
    <system.web>
        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
        <compilation debug="true"/>
        <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
        <authentication mode="Windows"/>
 
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
 
    </system.web>
</configuration>
 

On some Button click,after it modified like :
 
<?xml version="1.0"?>
<!--
    Note: As an alternative to hand editing this file you can use the
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->
 
<!--
    Note: As an alternative to hand editing this file you can use the
    \Windows\Microsoft.Net\Framework\v2.x\Config
-->
 

<configuration>
    <appSettings>
  <add key="customsetting1" value="Some text here" />
  <add key="myKey" value=",1,1,1,1,1,1" />
  <add key="RemoveTCPError" value="1,1" />
 </appSettings>
    <connectionStrings>
  <add name="myDatabaseName" connectionString="whatever" />
 </connectionStrings>
    <system.web>
        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
        <compilation debug="true"/>
        <!--
            The <authentication> section enables configuration
            of the security authentication mode used by
            ASP.NET to identify an incoming user.
        -->
        <authentication mode="Windows"/>
 
        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
 
    </system.web>
</configuration>
 

or any where in the web.config file commnets should be added or in any commented part text should be added.
Is this possible.
Please,Help me.
 
Thanks to all in Advance.
 
Regards,
Ranjeet Waje
Posted 4-Jun-12 20:47pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

any where in the web.config file commnets should be added or in any commented part text should be added.
Is this possible.

Possible, Yes! But I would not suggest to do it at all.
 
Web.Config is a configuration file based on which your website is running. Allowing anyone(even admin) to edit/update config file would need a restart of your application pool which would be an issue if someone is connected to your app at the same time somewhere else. Further, adding a "comment" to config file does not sounds anywhere important or necessary feature.
 
If you think otherwise, just find/locate the config file, and find the node and update it just as a normal XML. Do make sure, you have permission to edit/update the file.
  Permalink  
Comments
Manas Bhardwaj at 5-Jun-12 4:32am
   
well explained +5!
Sandeep Mewara at 5-Jun-12 4:40am
   
There is no 5 here - yet, Thanks. :)
Manas Bhardwaj at 5-Jun-12 4:41am
   
Oops. It is there now :)
Sandeep Mewara at 5-Jun-12 5:43am
   
:)
Espen Harlinn at 5-Jun-12 8:12am
   
5'ed!
Sandeep Mewara at 5-Jun-12 9:33am
   
Thanks Espen.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Have a look at the WebConfigurationManager class[^]. This class in turn provides access to another class[^], Configuration which will allow you to work and edit config files.
  Permalink  
v2
Comments
Manas Bhardwaj at 5-Jun-12 4:32am
   
this is correct. +5
Abhinav S at 5-Jun-12 5:34am
   
Thanks Manas.
Espen Harlinn at 5-Jun-12 8:12am
   
5'ed!
Abhinav S at 5-Jun-12 8:30am
   
Thank you Espen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 5 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100