我是如何动态编辑App.config的!
2004-09-13 16:42
519 查看
在工作中为了实现临时保存数据的目的,我选择了用App.config
本文假设App.config的预先设置内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Copy" value="ABC公司"/>
<add key="Company" value="无锡市ABC信息技术有限公司"/>
<add key="ComUrl" value="http://www.ABC.net.cn"/>
</appSettings>
</configuration>
读取我就不说了,很多人都会的!
引用:Imports System.Configuration名字空间
然后,
TBCopy.Text = ConfigurationSettings.AppSettings("Copy")
TBCompany.Text = ConfigurationSettings.AppSettings("Company")
TBComUrl.Text = ConfigurationSettings.AppSettings("ComUrl")
这样就调用了!
可是怎么样编辑呢?如何编辑它成了难题,首先在CSDN上找了很久,无果,到MSDN上也找了很久,不太适合,看来只有自己动手了! 因为其是个典型的XML文件,于是可以以XML的方法操作它!将下面的过程放到模块中或需要用到的地方!
===
VB.net
===
Public Sub SysConfig(ByVal myValue() As String)
Dim i As Integer
Dim XmlDoc As New XmlDocument
XmlDoc.Load(Application.ExecutablePath & ".config")
Dim XN As XmlNode = XmlDoc.SelectSingleNode("/configuration/appSettings")
For i = 0 To myValue.Length - 1
XN.ChildNodes.Item(i).Attributes.ItemOf(1).Value() = myValue(i)
Next
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
XmlDoc = Nothing
End Sub
===
C#.net
===
Public Void SysConfig(String myValue() )
{
XmlDocument XmlDoc =new XmlDocument();
XmlDoc.Load(Application.ExecutablePath & ".config");
XmlNode XN=new XmlNode();
XN.SelectSingleNode("/configuration/appSettings");
For (int i = 0 ;i<= myValue.Length - 1;i++)
{
XN.ChildNodes.Item(i).Attributes.ItemOf(1).Value() = myValue(i);
}
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlDoc.Dispose();
}
我是通过传递一个字符串数组来作参数的!每个值就是Config里的一行!按顺序排好!
Dim tmpStr() As String = {Trim(TBCopy.Text), Trim(TBCompany.Text), Trim(TBComUrl.Text)}
SysConfig(tmpStr)
这样就完成了修改的目的!
还有一种方法:
以Dataset方式读写Config及相关XML文件
Private Sub DealXML()
Dim myDs As New DataSet
myDs.ReadXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
TBCopy.Text = myDs.Tables(1).Rows(0)(1)
TBCompany.Text = myDs.Tables(1).Rows(1)(1)
TBComUrl.Text = myDs.Tables(1).Rows(2)(1)
'------------------------------------------
myDs.Tables(1).Rows(0)(1) = TBCopy.Text
myDs.Tables(1).Rows(1)(1) = TBCompany.Text
myDs.Tables(1).Rows(2)(1) = TBComUrl.Text
'------------------------------------------
myDs.AcceptChanges()
myDs.WriteXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
myDs.Clear()
myDs.Dispose()
End Sub
至于喜欢用哪种都行!呵呵,完!有问题请联系作者!liujiayu10@hotmail.com
本文假设App.config的预先设置内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Copy" value="ABC公司"/>
<add key="Company" value="无锡市ABC信息技术有限公司"/>
<add key="ComUrl" value="http://www.ABC.net.cn"/>
</appSettings>
</configuration>
读取我就不说了,很多人都会的!
引用:Imports System.Configuration名字空间
然后,
TBCopy.Text = ConfigurationSettings.AppSettings("Copy")
TBCompany.Text = ConfigurationSettings.AppSettings("Company")
TBComUrl.Text = ConfigurationSettings.AppSettings("ComUrl")
这样就调用了!
可是怎么样编辑呢?如何编辑它成了难题,首先在CSDN上找了很久,无果,到MSDN上也找了很久,不太适合,看来只有自己动手了! 因为其是个典型的XML文件,于是可以以XML的方法操作它!将下面的过程放到模块中或需要用到的地方!
===
VB.net
===
Public Sub SysConfig(ByVal myValue() As String)
Dim i As Integer
Dim XmlDoc As New XmlDocument
XmlDoc.Load(Application.ExecutablePath & ".config")
Dim XN As XmlNode = XmlDoc.SelectSingleNode("/configuration/appSettings")
For i = 0 To myValue.Length - 1
XN.ChildNodes.Item(i).Attributes.ItemOf(1).Value() = myValue(i)
Next
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
XmlDoc = Nothing
End Sub
===
C#.net
===
Public Void SysConfig(String myValue() )
{
XmlDocument XmlDoc =new XmlDocument();
XmlDoc.Load(Application.ExecutablePath & ".config");
XmlNode XN=new XmlNode();
XN.SelectSingleNode("/configuration/appSettings");
For (int i = 0 ;i<= myValue.Length - 1;i++)
{
XN.ChildNodes.Item(i).Attributes.ItemOf(1).Value() = myValue(i);
}
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlDoc.Dispose();
}
我是通过传递一个字符串数组来作参数的!每个值就是Config里的一行!按顺序排好!
Dim tmpStr() As String = {Trim(TBCopy.Text), Trim(TBCompany.Text), Trim(TBComUrl.Text)}
SysConfig(tmpStr)
这样就完成了修改的目的!
还有一种方法:
以Dataset方式读写Config及相关XML文件
Private Sub DealXML()
Dim myDs As New DataSet
myDs.ReadXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
TBCopy.Text = myDs.Tables(1).Rows(0)(1)
TBCompany.Text = myDs.Tables(1).Rows(1)(1)
TBComUrl.Text = myDs.Tables(1).Rows(2)(1)
'------------------------------------------
myDs.Tables(1).Rows(0)(1) = TBCopy.Text
myDs.Tables(1).Rows(1)(1) = TBCompany.Text
myDs.Tables(1).Rows(2)(1) = TBComUrl.Text
'------------------------------------------
myDs.AcceptChanges()
myDs.WriteXml(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
myDs.Clear()
myDs.Dispose()
End Sub
至于喜欢用哪种都行!呵呵,完!有问题请联系作者!liujiayu10@hotmail.com
相关文章推荐
- 我是如何动态编辑App.config的!
- 我是如何动态编辑App.config的
- easyui如何动态改变列的编辑属性
- 转:Android App 如何动态加载类
- 如何动态编辑fragment的高度,宽度和margin?
- 如何动态设置全局theme,及在web.config中读取pages节点的内容。
- .NET: 如何通过AppDomain动态加载插件程序
- 动态修改App.Config 和web.Config
- 动态修改App.Config 和web.Config
- 如何用jbpm做出一个状态可视化动态可编辑的工作流平台-1
- android 如何动态的加载类----app插件技术
- AppDomain以及如何改变web.config / App.config位置
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)4-----Net下的AppDomain编程 [摘录]
- Android App 如何动态加载类
- VSTS中如何ignore一个app.config之类的文件
- android 如何动态的加载类----app插件技术
- app.config动态修改及读取
- [转帖]动态读取App.Config
- Android插件化开发基础之App如何动态加载类
- android 如何动态的加载类----app插件技术