77 lines
2.4 KiB
VB.net
77 lines
2.4 KiB
VB.net
Imports System.IO
|
|
Imports NLog
|
|
|
|
Public MustInherit Class BaseConfig
|
|
Private Const _userConfigFileName As String = "UserConfig.xml"
|
|
Private _logFactory As LogFactory
|
|
Private _logger As Logger
|
|
|
|
Protected Const _configKey As String = "Key"
|
|
Protected Const _configValue As String = "Value"
|
|
Protected _dataTable As DataTable
|
|
|
|
Protected ReadOnly Property PropertyNames As Dictionary(Of String, String)
|
|
Get
|
|
Return New Dictionary(Of String, String)
|
|
End Get
|
|
End Property
|
|
|
|
Public Sub New(LogFactory As LogFactory)
|
|
_logFactory = LogFactory
|
|
_logger = LogFactory.GetCurrentClassLogger()
|
|
End Sub
|
|
|
|
Private Function GetUserConfigPath() As String
|
|
Dim oAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
|
|
Dim oCompanyName = My.Application.Info.CompanyName
|
|
Dim oProductName = My.Application.Info.ProductName
|
|
|
|
Return Path.Combine(oAppData, oCompanyName, oProductName, _userConfigFileName)
|
|
End Function
|
|
|
|
Protected Function LoadConfig() As DataTable
|
|
Dim oUserConfigPath As String = GetUserConfigPath()
|
|
Dim oDatatable As New DataTable()
|
|
|
|
If Not File.Exists(oUserConfigPath) Then
|
|
_logger.Warn("Config file {0} does not exist", oUserConfigPath)
|
|
Return Nothing
|
|
End If
|
|
|
|
Try
|
|
oDatatable.ReadXml(oUserConfigPath)
|
|
Return oDatatable
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Protected Sub SaveConfig(DataTable As DataTable)
|
|
Dim oUserConfigPath As String = GetUserConfigPath()
|
|
|
|
Try
|
|
DataTable.WriteXml(oUserConfigPath)
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Protected Function ConvertToDataTable(dictionary As Dictionary(Of String, String)) As DataTable
|
|
Dim oNewDataTable As New DataTable("Config")
|
|
oNewDataTable.Columns.Add(New DataColumn("Key", GetType(String)))
|
|
oNewDataTable.Columns.Add(New DataColumn("Value", GetType(String)))
|
|
|
|
For Each oProperty In dictionary
|
|
Dim oNewRow = oNewDataTable.NewRow()
|
|
oNewRow.Item("Key") = oProperty.Key
|
|
oNewRow.Item("Value") = oProperty.Value
|
|
oNewDataTable.Rows.Add(oNewRow)
|
|
Next
|
|
|
|
oNewDataTable.AcceptChanges()
|
|
|
|
Return oNewDataTable
|
|
End Function
|
|
End Class
|