配置文件一般分为内置配置文和用户自定义配置文件。
内置配置文件包括app.config、web.config、Settings.settings等等。
用户自定义配置文件一般是将配置信息放到XML文件或注册表中,配置信息一般包括程序设置,记录运行信息,保存控件的信息(比如位置,样式)等。
今天就来看看app.config配置文件的读写!各位看官请直观代码吧!
代码语言:javascript复制使用示例:
Public Class Form1
Public Sub New()
InitializeComponent()
AppConfig.Appconfig_init()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(AppConfig.Insert_Config("bb", "89898", AppConfig.App_mode.app_config))
'MsgBox(AppConfig.GET_Config("SQLiteDB", AppConfig.App_mode.connection_config))
End Sub
End Class
类模块代码
代码语言:javascript复制''' <summary>
''' AppConfig节点配置
''' </summary>
Public Class AppConfig
Public Shared Appfig As AppConfig = Nothing
Private Shared ConFig As Object
Private Shared App_set As Configuration.AppSettingsSection
Private Shared App_connection As Configuration.ConnectionStringsSection
''' <summary>
''' 类型分配
''' </summary>
Enum App_mode
''' <summary>
''' app配置
''' </summary>
app_config = 1
''' <summary>
''' 连接配置
''' </summary>
connection_config = 2
End Enum
Public Sub New()
End Sub
''' <summary>
''' 初始化AppConfig
''' </summary>
''' <returns></returns>
Public Shared Function Appconfig_init() As AppConfig
Try
ConFig = Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)
App_connection = ConFig.ConnectionStrings
App_set = ConFig.AppSettings
If Appfig Is Nothing Then Appfig = New AppConfig
Return Appfig
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' 读取app.config节点值
''' </summary>
''' <param name="KEYSTR"></param>
''' <returns></returns>
Public Shared Function GET_Config(ByVal KEYSTR As String, ByVal mode As App_mode) As String
Try
Dim Temp As String = Nothing
Select Case mode
Case 1
Temp = Configuration.ConfigurationManager.AppSettings(KEYSTR)
Case 2
Temp = Configuration.ConfigurationManager.ConnectionStrings(KEYSTR).ConnectionString
End Select
Return Temp
Catch ex As Exception
Return Nothing
End Try
End Function
''' <summary>
''' 添加Appsetting节点值
''' </summary>
''' <param name="KEYSTR">key键</param>
''' <param name="VALSTR">value值</param>
''' <returns></returns>
Public Shared Function Insert_Config(ByVal KEYSTR As String, ByVal VALSTR As String, ByVal mode As App_mode) As Boolean
Try
Select Case mode
Case 1
If Configuration.ConfigurationManager.AppSettings(KEYSTR) = Nothing Then
App_set.Settings.Add(New Configuration.KeyValueConfigurationElement(KEYSTR, VALSTR))
Else
Return False
End If
Case 2
App_connection.ConnectionStrings.Add(New Configuration.ConnectionStringSettings(KEYSTR, VALSTR))
End Select
ConFig.Save(Configuration.ConfigurationSaveMode.Modified)
Configuration.ConfigurationManager.RefreshSection("appSettings")
Configuration.ConfigurationManager.RefreshSection("connectionStrings")
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 修改节点值(注意:修改后,App.config文件中节点值没有更改,而是exe.config的配置值更改,不影响正常读取)
''' </summary>
''' <param name="KEYSTR">key键</param>
''' <param name="VALSTR">value值</param>
''' <returns></returns>
Public Shared Function Fix_Config(ByVal KEYSTR As String, ByVal VALSTR As String, ByVal mode As App_mode) As Boolean
Try
Select Case mode
Case 1
App_set.Settings(KEYSTR).Value = VALSTR
Case 2
App_connection.ConnectionStrings(KEYSTR).ConnectionString = VALSTR
End Select
ConFig.Save(Configuration.ConfigurationSaveMode.Modified)
Configuration.ConfigurationManager.RefreshSection("appSettings")
Configuration.ConfigurationManager.RefreshSection("connectionStrings")
Return True
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' 删除appconfig节点值
''' </summary>
''' <param name="KEYSTR">key键</param>
''' <returns></returns>
Public Shared Function Del_Config(ByVal KEYSTR As String, ByVal mode As App_mode) As Boolean
Try
Select Case mode
Case 1
App_set.Settings.Remove(KEYSTR)
Case 2
App_connection.ConnectionStrings.Remove(KEYSTR)
End Select
ConFig.Save(Configuration.ConfigurationSaveMode.Modified)
Configuration.ConfigurationManager.RefreshSection("appSettings")
Configuration.ConfigurationManager.RefreshSection("connectionStrings")
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
各位看官今天代码就分享到这了!
有问题请指正!