C# 以编程方式更新web配置文件时,注释将被删除
有问题地更新web.config时,注释和某些键被删除C# 以编程方式更新web配置文件时,注释将被删除,c#,asp.net,c#-4.0,C#,Asp.net,C# 4.0,有问题地更新web.config时,注释和某些键被删除 string path = ConfigurationManager.AppSettings["path"]; var map = new ExeConfigurationFileMap { ExeConfigFilename = path }; var configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None)
string path = ConfigurationManager.AppSettings["path"];
var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
var configFile = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
configFile.AppSettings.Settings["StrKeyName"].Value = Convert.ToString(IntValue);
configFile.Save();
请允许我知道为什么会发生这样的事情,我想保留这些评论。请让我知道我如何才能做到这一点 没有办法维护这些评论。这就是.NET实现它的方式。 但是您可以拥有自己的库,它可以帮助您使用XmlDocument类操作配置文件 我创建了一个类似的类。下面是VB.NET中的类代码
Imports System.Xml
Imports System.Configuration
Public Class XConfiguration
Private ConfigurationFilePath As String
Private XmlDoc As XmlDocument
Public Sub New()
ConfigurationFilePath = Web.HttpContext.Current.Server.MapPath("/") & "web.config"
XmlDoc = New XmlDocument() With {.PreserveWhitespace = True}
Try
XmlDoc.Load(ConfigurationFilePath)
Catch e As System.IO.FileNotFoundException
Throw New Exception("No configuration file found.", e)
End Try
End Sub
Public Sub WriteConnectionString(ByVal Name As String, ByVal ConnectionString As String,Optional ByVal SaveImmediately As Boolean = False)
Dim NodeConnectionStrings As XmlNode = XmlDoc.SelectSingleNode("//connectionStrings")
If NodeConnectionStrings Is Nothing Then Throw New InvalidOperationException("connectionStrings section not found in config file.")
Dim ElemAdd As XmlElement = CType(NodeConnectionStrings.SelectSingleNode(String.Format("//add[@name='{0}']", Name)), XmlElement)
If ElemAdd IsNot Nothing Then
ElemAdd.SetAttribute("connectionString", ConnectionString)
Else
ElemAdd = XmlDoc.CreateElement("add")
ElemAdd.SetAttribute("name", Name)
ElemAdd.SetAttribute("connectionString", ConnectionString)
NodeConnectionStrings.AppendChild(ElemAdd)
End If
If SaveImmediately Then Save()
End Sub
Public Function ReadSetting(ByVal Key As String) As String
Return ConfigurationManager.AppSettings(Key)
End Function
Public Sub WriteAppSetting(ByVal Key As String, ByVal Value As String, Optional ByVal SaveImmediately As Boolean = False)
Dim NodeAppSettings As XmlNode = XmlDoc.SelectSingleNode("//appSettings")
If NodeAppSettings Is Nothing Then Throw New InvalidOperationException("appSettings section not found in config file.")
Dim ElemAdd As XmlElement = CType(NodeAppSettings.SelectSingleNode(String.Format("//add[@key='{0}']", Key)), XmlElement)
If ElemAdd IsNot Nothing Then
ElemAdd.SetAttribute("value", Value)
Else
ElemAdd = XmlDoc.CreateElement("add")
ElemAdd.SetAttribute("key", Key)
ElemAdd.SetAttribute("value", Value)
NodeAppSettings.AppendChild(ElemAdd)
End If
If SaveImmediately Then Save()
End Sub
Public Sub RemoveSetting(ByVal Key As String, Optional ByVal SaveImmediately As Boolean = False)
Dim NodeAppSettings As XmlNode = XmlDoc.SelectSingleNode("//appSettings")
If NodeAppSettings Is Nothing Then
Throw New InvalidOperationException("appSettings section not found in config file.")
Else
NodeAppSettings.RemoveChild(NodeAppSettings.SelectSingleNode(String.Format("//add[@key='{0}']", Key)))
End If
If SaveImmediately Then Save()
End Sub
Public Sub Save()
XmlDoc.Save(ConfigurationFilePath)
End Sub
End Class
好的,很好的发现。现在,你有问题吗?不清楚你是否在问什么。@Tom:LOL问题更新了。可能是重复的