Can not Modify web.config file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pasam
    New Member
    • Apr 2009
    • 3

    Can not Modify web.config file

    Hi
    I am creating installer for asp.net

    i am using this code for to modify web.config file
    but i cant whats wrong this code

    Code:
    ' Get configuration.
            Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath)
    
            ' Find app setting to change
            Dim element As KeyValueConfigurationElement = CType(config.AppSettings.Settings("ConnectString"), KeyValueConfigurationElement)
            If Not element Is Nothing Then
                element.Value = strCon
            End If
            config.Save()
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    What error message are you getting?

    Comment

    • pasam
      New Member
      • Apr 2009
      • 3

      #3
      I didnt get any eroor message when i instaling.

      but no modification in web.config file.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You need to load the web.config into memory before you can modify it.
        It's probably a good idea load it as an XmlDocument so that you can parse it easily and edit the key that you want to.

        For example:
        Code:
        'The webConfigLocation variable should be the path to the web.config file.
          Dim webConfigLocation As String = "C:\MyWebAppPath\web.config"
          Dim webConfigDoc As New XmlDocument
          webConfigDoc.Load(webConfigLocation)
          Dim appConfigList As XmlNodeList = webConfigDoc.GetElementsByTagName("add")
        
           For Each appConfigElement As XmlNode In appConfigList
              Dim key As String
              If Not appConfigElement.Attributes("key") Is Nothing Then
                 key = appConfigElement.Attributes("key").Value
        
        'Where "DatabaseConnection" is the name of the key for your Connection String
                 If String.Compare(key, "DatabaseConnection", True) = 0 Then
                    appConfigElement.Attributes("value").Value = "Some Edited Value"
                    Exit For
                 End If
              End If
           Next
           webConfigDoc.PreserveWhitespace = True
           webConfigDoc.Save(webConfigLocation)

        Comment

        • pasam
          New Member
          • Apr 2009
          • 3

          #5
          Thnaks for your code

          I will try this

          Comment

          Working...