Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear All,

Hi, I want add some comments in my web.config file at runtime.
Means for example

original Web.config file is:

C#
<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
<?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
Updated 10-Jan-22 22:47pm

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.
 
Share this answer
 
Comments
Manas Bhardwaj 5-Jun-12 4:32am    
well explained +5!
Sandeep Mewara 5-Jun-12 4:40am    
Thanks. :)
Manas Bhardwaj 5-Jun-12 4:41am    
Oops. It is there now :)
Sandeep Mewara 5-Jun-12 5:43am    
:)
Espen Harlinn 5-Jun-12 8:12am    
5'ed!
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.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 5-Jun-12 4:32am    
this is correct. +5
Abhinav S 5-Jun-12 5:34am    
Thanks Manas.
Espen Harlinn 5-Jun-12 8:12am    
5'ed!
Abhinav S 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900