很多时候我们需要对系统的.config文件进度读写操作,例如:系统初始化的参数的更改、系统参数的改变都需要更新到配置文件。
首先我们有必要了解一下app.config、exe.config和vshost.exe.config作用和区别:
vshost.exe.config是程序运行时的配置文本,exe.config是程序运行后会复制到vshost.exe.config,app.config是在vshost.exe.config和exe.config没有情况起作用,从app.config复制到exe.config再复制到vshost.exe.config。vshost.exe.config和exe.config会自动创建内容跟app.config一样。了解过这些其实写配置文件都是写到exe.config文件中了,app.config不会变化。网上也有许多关于配置文件的读写操作,也是借鉴了多位前辈的经验自己总结的一些比较常用的读写操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<pre class="inline:true class:language-dotnet decode:1 " >''' <summary> ''' 获取AppSettings中某一节点值 ''' </summary> ''' <param name="key"></param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function GetConfigValue(ByVal key As String) As String Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) If config.AppSettings.Settings(key) IsNot Nothing Then Return config.AppSettings.Settings(key).Value Else Return String.Empty End If End Function ''' <summary> ''' 修改AppSettings中配置 ''' </summary> ''' <param name="key">key值</param> ''' <param name="value">相应值</param> ''' <returns></returns> ''' <remarks></remarks> Shared Function SetConfigValue(ByVal key As String, ByVal value As String) As Boolean Try Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) If config.AppSettings.Settings(key) IsNot Nothing Then config.AppSettings.Settings(key).Value = value Else config.AppSettings.Settings.Add(key, value) End If config.Save(ConfigurationSaveMode.Modified) ConfigurationManager.RefreshSection("appSettings") Return True Catch Return False End Try End Function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<pre class="inline:true class:language-dotnet decode:1 " >''' <summary> ''' 获取连接节点值 ''' </summary> ''' <param name="key"></param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function GetConnectionValue(ByVal key As String) As String If ConfigurationManager.ConnectionStrings(key) IsNot Nothing Then Return ConfigurationManager.ConnectionStrings(key).ConnectionString Return String.Empty End Function ''' <summary> ''' 修改或新增ConnectionStrings节点 ''' </summary> ''' <param name="key"></param> ''' <param name="conString"></param> ''' <remarks></remarks> Public Shared Sub UpdateConnectionStringsConfig(ByVal key As String, ByVal conString As String) Dim isModified As Boolean = False '记录该连接串是否已经存在 If ConfigurationManager.ConnectionStrings(key) IsNot Nothing Then isModified = True End If '新建一个连接字符串实例 Dim mySettings As ConnectionStringSettings = New ConnectionStringSettings(key, conString) '打开可执行的配置文件*.exe.config Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) '如果连接串已存在,首先删除它 If isModified Then config.ConnectionStrings.ConnectionStrings.Remove(key) End If '将新的连接串添加到配置文件中. config.ConnectionStrings.ConnectionStrings.Add(mySettings) '保存对配置文件所作的更改 config.Save(ConfigurationSaveMode.Modified) '强制重新载入配置文件的ConnectionStrings配置节 ConfigurationManager.RefreshSection("connectionStrings") End Sub |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<pre class="inline:true class:language-dotnet decode:1 " >''' <summary> ''' 读取EndpointAddress ''' </summary> ''' <param name="endpointName"></param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function GetEndpointClientAddress(ByVal endpointName As String) As String Dim clientSection As ClientSection = TryCast(ConfigurationManager.GetSection("system.serviceModel/client"), ClientSection) For Each item As ChannelEndpointElement In clientSection.Endpoints If item.Name = endpointName Then Return item.Address.ToString() Next Return String.Empty End Function ''' <summary> ''' 设置EndpointAddress ''' </summary> ''' <param name="endpointName"></param> ''' <param name="address"></param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function SetEndpointClientAddress(ByVal endpointName As String, ByVal address As String) As Boolean Try Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) Dim clientSection As ClientSection = TryCast(config.GetSection("system.serviceModel/client"), ClientSection) For Each item As ChannelEndpointElement In clientSection.Endpoints If item.Name <> endpointName Then Continue For item.Address = New Uri(address) Exit For Next config.Save(ConfigurationSaveMode.Modified) ConfigurationManager.RefreshSection("system.serviceModel") Return True Catch ex As Exception Return False End Try End Function |
浏览量: 11