Page view counter

(Writing to Web.config file dynamically

Last post 07-02-2008 1:02 AM by hannous. 14 replies.

Sort Posts:

  • (Writing to Web.config file dynamically

    01-10-2007, 1:52 AM
    • Loading...
    • amersafi
    • Joined on 07-13-2006, 11:45 AM
    • Posts 83
    • Points 237

    i have web.config that have more than value like this

    <

    appSettings>

     

    <

    add key="lang" value="en"/>

    <

    add key="langCode" value="cmsv9_lang"/>

    <

    add key="SiteURL" value="/CMS9_Ajax_Website"/>

    <

    add key="RealURL" value="/10.10.0.253"/>

    <

    add key="SiteUserUrl" value="/User"/>

    <

    add key="SiteAdminUrl" value="/Admin"/>

    <

    add key="ImageUrl" value="/Images"/>

    <

    add key="ScriptUrl" value="/JS"/>

    <

    add key="WordsFilePath" value="/App_Data/xml/Localization_word.xml"/>

    <

    add key="LanguagesFilePath" value="/App_Data/xml/Localization_Languages.xml"/>

     

     

    </

    appSettings>

    i can read this like this

     

    Dim i As Integer = 0

    Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)

    Dim appSettings As AppSettingsSection = CType(config.GetSection("appSettings"), AppSettingsSection)

    Dim appKeys As String() = appSettings.Settings.AllKeys

    For i = 0 To appSettings.Settings.Count - 1

    objHT.Add(appSettings.Settings(appKeys(i)).Key, appSettings.Settings(appKeys(i)).Value)

    Next

    but my question how can i update or add value in web.config dynamically from code

  • Re: (Writing to Web.config file dynamically

    01-10-2007, 6:59 AM
    • Loading...
    • Hope4sun
    • Joined on 12-05-2006, 3:53 PM
    • UK
    • Posts 201
    • Points 1,139
    This article covers the code to do it pretty much http://www.west-wind.com/WebLog/posts/8461.aspx but as stated, you need high trust permissions on the server to do it.
  • Re: (Writing to Web.config file dynamically

    01-10-2007, 7:50 AM
    Answer
    • Loading...
    • hannous
    • Joined on 11-29-2006, 8:19 AM
    • Paris, France
    • Posts 56
    • Points 203

    Here's how you can add a key:

        Dim FileLocation As String = "C:\Inetpub\wwwroot\YourProject\web.config"
        Dim NodeLocation As String = "configuration/appSettings"
    
        'Adds a key and value to the configuration file
        Public Function AddKey(ByVal strKey As String, ByVal strValue As String) As Boolean
            Dim xmlDoc As New XmlDocument
            'Change this to the location of your configuration file
            xmlDoc.Load(FileLocation)
            'Change this if node is different
            Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation)
            Try
                'Check if the node exists before adding it
                If (KeyExists(strKey)) Then
                    Throw New ArgumentException("Key name: <" + strKey + "> already exists in the configuration.")
                End If
                'You must have at least one key that can be empty
                Dim newChild As XmlNode = appSettingsNode.FirstChild.Clone
                newChild.Attributes("key").Value = strKey
                newChild.Attributes("value").Value = strValue
                appSettingsNode.AppendChild(newChild)
                xmlDoc.Save(FileLocation )
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function
    
        'Determines if a key exists
        Public Function KeyExists(ByVal strKey As String) As Boolean
            Dim xmlDoc As New XmlDocument
            xmlDoc.Load(FileLocation )
            Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation)
            Dim childNode As XmlNode
            For Each childNode In appSettingsNode
                If (childNode.Attributes("key").Value = strKey) Then
                    Return True
                End If
            Next
            Return False
        End Function
     
  • Re: (Writing to Web.config file dynamically

    01-10-2007, 8:43 AM
    • Loading...
    • amersafi
    • Joined on 07-13-2006, 11:45 AM
    • Posts 83
    • Points 237
    thank u that helped me
  • Re: (Writing to Web.config file dynamically

    09-18-2007, 6:59 AM
    • Loading...
    • hajaworld
    • Joined on 09-18-2007, 10:51 AM
    • Chennai
    • Posts 12
    • Points 24

    hi i am tried your concept,but it arise error when execute save  line

     error is;Access to the path is denied

    help me

    hajaworld@yahoo.co.in 

    by
    Haja
    mail id:hajaworld@yahoo.co.in
    Dotnet Developer
  • Re: (Writing to Web.config file dynamically

    09-18-2007, 7:09 AM
    • Loading...
    • hannous
    • Joined on 11-29-2006, 8:19 AM
    • Paris, France
    • Posts 56
    • Points 203
    Are you sure you have write access to that path?

     

  • Re: (Writing to Web.config file dynamically

    10-18-2007, 1:34 AM

    This may help

    Use configSource attribute to manage Web.Config sections in ASP.Net 2.0

    http://rizwanshah.blogspot.com/2007/10/use-configsource-attribute-to-manage.html

     

  • Re: (Writing to Web.config file dynamically

    06-25-2008, 5:42 AM
    • Loading...
    • kanthu2681
    • Joined on 06-25-2008, 9:32 AM
    • Posts 14
    • Points 24

    Here is my web.config file that i'm trying to update dynamically using Hannous code and I receive null as XmlNode. But this works only if i remove xmlns from the web.config. Could you please help me out as it is very urgent for me.

    <?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <configSections>
       
      <appSettings>
        <!-- point to the database you want to use-->
        <add key="ConnStr" value="Server=servername; database=databasename;Integrated Security=SSPI;Connect Timeout=0" />
        <!-- specify the full path of the rolespace.xml-->
        <add key="ROLESPACE" value="E:\webroot\OAWebService\oaweb.xml" />
        <add key="TOCLevelDepth" value="3" />
        <add key="SearchMax" value="500" />
      </appSettings>
     
    </configuration>

     

    Thanks!!!

  • Re: (Writing to Web.config file dynamically

    06-25-2008, 11:45 AM
    • Loading...
    • hannous
    • Joined on 11-29-2006, 8:19 AM
    • Paris, France
    • Posts 56
    • Points 203

    To care for the namespace, add the following:

    After:

     

    xmlDoc.Load(FileLocation)
     

    Add:

     

    Dim XmlNamespaceManager As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmlDoc.NameTable)
    XmlNamespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0")
     
  • Re: (Writing to Web.config file dynamically

    06-26-2008, 5:34 AM
    • Loading...
    • kanthu2681
    • Joined on 06-25-2008, 9:32 AM
    • Posts 14
    • Points 24

    Hi Thanks for your prompt response. But It did not worked still the value is giving null for appsettingsNode.

    In the web.config file the namespace prefix is xmlns(which is reserve key word) and in the code we are using 'ns' as the prefix. Hope coz of this it failed.

    Kindly, let me know where i'm going wrong.

     

  • Re: (Writing to Web.config file dynamically

    06-27-2008, 5:24 PM
    • Loading...
    • hannous
    • Joined on 11-29-2006, 8:19 AM
    • Paris, France
    • Posts 56
    • Points 203

    Hi,

    No that's not the problem. Actually I forgot to tell you that you should add NameSpaceManager to SelectSingleNode, therefore changing:

     

    Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation)
     

    to:

     

    Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation, XmlNamespaceManager)
     
  • Re: (Writing to Web.config file dynamically

    06-30-2008, 4:07 AM
    • Loading...
    • kanthu2681
    • Joined on 06-25-2008, 9:32 AM
    • Posts 14
    • Points 24

    Hi hannous, I am still not able to succeed and kindly help me to succeed with this problem. Here is my code and xml file.

    Imports System.Xml

    Module Module1

    Dim FileLocation As String = "C:\webroot\web.config"

    Dim NodeLocation As String = "configuration/appSettings"

    Sub Main()

     

    Dim x As Boolean = AddKey(FileLocation, NodeLocation)

    If (x) Then

    Console.WriteLine("success")

    Else

    Console.WriteLine("failed")

    End If

    'Adds a key and value to the configuration file

    End Sub

    Public Function AddKey(ByVal strKey As String, ByVal strValue As String) As Boolean

    Dim xmlDoc As New XmlDocument

    'Change this to the location of your configuration file

    xmlDoc.Load(FileLocation)

    Dim XmlNamespaceManager As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmlDoc.NameTable)

    XmlNamespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0")

    'Change this if node is different

    Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation, XmlNamespaceManager)

    Try

    'Check if the node exists before adding it

    If (KeyExists(strKey)) Then

    Throw New ArgumentException("Key name: <" + strKey + "> already exists in the configuration.")

    End If

    'You must have at least one key that can be empty

    Dim newChild As XmlNode = appSettingsNode.FirstChild.Clone

    newChild.Attributes("key").Value = strKey

    newChild.Attributes("value").Value = strValue

    appSettingsNode.AppendChild(newChild)

    xmlDoc.Save(FileLocation)

    Return True

    Catch ex As Exception

    Return False

    End Try

    End Function

    'Determines if a key exists

    Public Function KeyExists(ByVal strKey As String) As Boolean

    Dim xmlDoc As New XmlDocument

    xmlDoc.Load(FileLocation)

    Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation)

    Dim childNode As XmlNode

    For Each childNode In appSettingsNode

    If (childNode.Attributes("key").Value = strKey) Then

    Return True

    End If

    Next

    Return False

    End Function

    End Module

     Here is the XML file code:

    <?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

       
      <appSettings>
        <!-- point to the database you want to use-->
        <add key="ConnStr" value="Server=servername; database=databasename;Integrated Security=SSPI;Connect Timeout=0" />
        <!-- specify the full path of the rolespace.xml-->
        <add key="ROLESPACE" value="E:\webroot\OAWebService\oaweb.xml" />
        <add key="TOCLevelDepth" value="3" />
        <add key="SearchMax" value="500" />
      </appSettings>
     
    </configuration>

    -----------

    I am receiving nothing as the value for appsettingsNode variable. Kindly, let me know where i'm going wrong and help me out.

     

  • Re: (Writing to Web.config file dynamically

    06-30-2008, 6:03 PM
    • Loading...
    • hannous
    • Joined on 11-29-2006, 8:19 AM
    • Paris, France
    • Posts 56
    • Points 203

    Your problem is that the node "configuration/appSettings" is not being recognized because of the namespace. What you need to do is first go the node "configuration" then go to "appSettings". To do so, set the NodeLocation first to "configuration" (without /appSettings), then, after this line:

     

    Dim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode(NodeLocation, XmlNamespaceManager)
     

    try:

      

    appSettingsNode = appSettingsNode.FirstChild
     

    Then keep the rest of the code as it is. As long as appSettings is the first child node under the configuration node (normal situation), this should work.

  • Re: (Writing to Web.config file dynamically

    07-01-2008, 12:30 AM
    • Loading...
    • kanthu2681
    • Joined on 06-25-2008, 9:32 AM
    • Posts 14
    • Points 24

    Hi hannous. Thanks for your time.

    This did not work. Is there any alternative. I'm also trying with System.Configuration namespace but i'm facing the same problem there. Need ur help. Also,let me know when u'll be online (Your gmail/yahoo account) so that we can both work at same time to resolve this issue

  • Re: (Writing to Web.config file dynamically

    07-02-2008, 1:02 AM
    • Loading...
    • hannous
    • Joined on 11-29-2006, 8:19 AM
    • Paris, France
    • Posts 56
    • Points 203

    I'm sorry you still have one more thing to do, add //ns: before NodeLocation on that previous line, so you'll have:

      

    NodeLocationDim appSettingsNode As XmlNode = xmlDoc.SelectSingleNode("//ns:" + NodeLocation, XmlNamespaceManager)
    appSettingsNode = appSettingsNode.FirstChild
     

    I tried your code with this and it worked.

Page 1 of 1 (15 items)