×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

What language do you use? If it's VB.NET here is a class I coded a while ago.

本文发表在 rolia.net 枫下论坛Imports System
Imports System.Xml
Imports System.Configuration
Imports System.Collections
Imports System.Reflection
Imports System.Diagnostics

Public Class appConfig

Private node As XmlNode = Nothing
Public docName As String = ""

Public Function SetValue(ByVal key As String, ByVal value As String) As Boolean

Dim cfgDoc As XmlDocument = New XmlDocument()
loadConfigDoc(cfgDoc)

'retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings")

If (node Is Nothing) Then
Throw New System.InvalidOperationException("appSettings section not found")
End If

Try
'XPath select setting "add" element that contains this key
Dim addElem As XmlElement = node.SelectSingleNode("//add[@key='" + key + "']")
If (addElem.HasAttributes) Then
addElem.SetAttribute("value", value)
' not found, so we need to add the element, key and value
Else
Dim entry As XmlElement = cfgDoc.CreateElement("add")
entry.SetAttribute("key", key)
entry.SetAttribute("value", value)
node.AppendChild(entry)
End If
'save it
saveConfigDoc(cfgDoc, docName)
Return True
Catch
Return False
End Try

End Function
Public Function removeElement(ByVal elementKey As String) As Boolean

Try
Dim cfgDoc As XmlDocument = New XmlDocument()
loadConfigDoc(cfgDoc)
'retrieve the appSettings node
node = cfgDoc.SelectSingleNode("//appSettings")
If (node Is Nothing) Then
Throw New System.InvalidOperationException("appSettings section not found")
Else
'XPath select setting "add" element that contains this key to remove
node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"))

saveConfigDoc(cfgDoc, docName)
End If
Return True
Catch
Return False
End Try

End Function
Private Function saveConfigDoc(ByVal cfgDoc As XmlDocument, ByVal cfgDocPath As String) As Boolean
Try
Dim writer As XmlTextWriter = New XmlTextWriter(cfgDocPath, System.Text.Encoding.ASCII)
writer.Formatting = Formatting.Indented
cfgDoc.WriteTo(writer)
writer.Flush()
writer.Close()
Return True
Catch
Return False
End Try
End Function
Private Function loadConfigDoc(ByVal cfgDoc As XmlDocument) As Boolean
Try
docName = System.AppDomain.CurrentDomain.BaseDirectory.ToString
docName += Reflection.Assembly.GetEntryAssembly.GetName.Name
docName += ".exe.config"
cfgDoc.Load(docName)
Return True
Catch e As Exception
Debug.Write(e.Message)
Return False
End Try
End Function

End Class
_________________________________________________
Let me know if it's useful更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / [Q]how to modify web.config from another web application?
    I tried to modify the web.config from another web application. I got access deny exception.

    Any advice is appreciated.
    • 如果不考虑安全问题,我会先设置给ASPNET用户对web.config文件的读写权限。
      • Have you ever done it. I tried and did not work. I think IIS should not allow to do so. Web.Config is different from other files.
        • 我只听说可以做到和不建议这么做,但没做过。我想如果给运行ASP.NET application的用户足够的权限,应该就不会有权限问题了吧。加上修改权限
    • What language do you use? If it's VB.NET here is a class I coded a while ago.
      本文发表在 rolia.net 枫下论坛Imports System
      Imports System.Xml
      Imports System.Configuration
      Imports System.Collections
      Imports System.Reflection
      Imports System.Diagnostics

      Public Class appConfig

      Private node As XmlNode = Nothing
      Public docName As String = ""

      Public Function SetValue(ByVal key As String, ByVal value As String) As Boolean

      Dim cfgDoc As XmlDocument = New XmlDocument()
      loadConfigDoc(cfgDoc)

      'retrieve the appSettings node
      node = cfgDoc.SelectSingleNode("//appSettings")

      If (node Is Nothing) Then
      Throw New System.InvalidOperationException("appSettings section not found")
      End If

      Try
      'XPath select setting "add" element that contains this key
      Dim addElem As XmlElement = node.SelectSingleNode("//add[@key='" + key + "']")
      If (addElem.HasAttributes) Then
      addElem.SetAttribute("value", value)
      ' not found, so we need to add the element, key and value
      Else
      Dim entry As XmlElement = cfgDoc.CreateElement("add")
      entry.SetAttribute("key", key)
      entry.SetAttribute("value", value)
      node.AppendChild(entry)
      End If
      'save it
      saveConfigDoc(cfgDoc, docName)
      Return True
      Catch
      Return False
      End Try

      End Function
      Public Function removeElement(ByVal elementKey As String) As Boolean

      Try
      Dim cfgDoc As XmlDocument = New XmlDocument()
      loadConfigDoc(cfgDoc)
      'retrieve the appSettings node
      node = cfgDoc.SelectSingleNode("//appSettings")
      If (node Is Nothing) Then
      Throw New System.InvalidOperationException("appSettings section not found")
      Else
      'XPath select setting "add" element that contains this key to remove
      node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"))

      saveConfigDoc(cfgDoc, docName)
      End If
      Return True
      Catch
      Return False
      End Try

      End Function
      Private Function saveConfigDoc(ByVal cfgDoc As XmlDocument, ByVal cfgDocPath As String) As Boolean
      Try
      Dim writer As XmlTextWriter = New XmlTextWriter(cfgDocPath, System.Text.Encoding.ASCII)
      writer.Formatting = Formatting.Indented
      cfgDoc.WriteTo(writer)
      writer.Flush()
      writer.Close()
      Return True
      Catch
      Return False
      End Try
      End Function
      Private Function loadConfigDoc(ByVal cfgDoc As XmlDocument) As Boolean
      Try
      docName = System.AppDomain.CurrentDomain.BaseDirectory.ToString
      docName += Reflection.Assembly.GetEntryAssembly.GetName.Name
      docName += ".exe.config"
      cfgDoc.Load(docName)
      Return True
      Catch e As Exception
      Debug.Write(e.Message)
      Return False
      End Try
      End Function

      End Class
      _________________________________________________
      Let me know if it's useful更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Thank you for the reply. Indeed, I wask asking how to set up security and configuration to allow aother application to modify the web.config file.
        I am not quite sure if we can do so or not.