用代碼修改Web.Config裏的鍵值

原則與操作XML文件相同。

    Private Sub SaveSetting(ByVal strKeyName As String, ByVal strKeyValue As String, ByVal strWhich As String)

        '創建XML文檔實例
        Dim XMLWebSetting As System.Xml.XmlDocument = New System.Xml.XmlDocument
        '打開XML文檔
        XMLWebSetting.Load(System.Web.HttpContext.Current.Server.MapPath("~/" + strWhich + ""))

        '查找節點所在位置
        Dim XmlNodeList As System.Xml.XmlNodeList = XMLWebSetting.SelectSingleNode("//appSettings").ChildNodes
        Dim xn As System.Xml.XmlNode
        Dim xe As System.Xml.XmlElement
        Try
            For Each xn In XmlNodeList
                xe = CType(xn, System.Xml.XmlElement)
                If xe.Attributes("key").InnerText = strKeyName Then
                    xe.Attributes("value").InnerText = strKeyValue
                    XMLWebSetting.Save(System.Web.HttpContext.Current.Server.MapPath("~/" + strWhich + ""))
                    Exit For
                End If
            Next
        Catch
        End Try
    End Sub 

 

過程使用:SaveSetting("ConStr", Trim(TextBox1.Text), "web.config")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章