config in client suite
This commit is contained in:
@@ -73,7 +73,6 @@
|
||||
<Import Include="System.Threading.Tasks" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="OldConfig.vb" />
|
||||
<Compile Include="ConfigManager.vb" />
|
||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||
<Compile Include="My Project\Application.Designer.vb">
|
||||
|
||||
@@ -12,7 +12,7 @@ Public Class ConfigManager(Of T)
|
||||
Private ReadOnly _File As Filesystem.File
|
||||
Private ReadOnly _UserPath As String
|
||||
Private ReadOnly _ComputerPath As String
|
||||
Private ReadOnly _CurrentDataPath As String
|
||||
Private _CurrentDataPath As String
|
||||
|
||||
Private ReadOnly _Schema As T
|
||||
Private ReadOnly _Serializer As XmlSerializer
|
||||
@@ -53,25 +53,15 @@ Public Class ConfigManager(Of T)
|
||||
Throw New DirectoryNotFoundException($"Path {ComputerConfigPath} does not exist!")
|
||||
End If
|
||||
|
||||
If Not Filesystem.File Then Then
|
||||
Throw New DirectoryNotFoundException()
|
||||
If Not _File.TestPathIsDirectory(UserConfigPath) Then
|
||||
Throw New ArgumentException($"Path {UserConfigPath} is not a directory!")
|
||||
End If
|
||||
|
||||
If IO.File.Exists(_UserPath) Then
|
||||
_Logger.Debug("Loading config from UserPath: {0}", _UserPath)
|
||||
_CurrentDataPath = _UserPath
|
||||
_Config = ReadFromFile(_UserPath)
|
||||
ElseIf IO.File.Exists(_ComputerPath) Then
|
||||
_Logger.Debug("Loading config from ComputerPath: {0}", _ComputerPath)
|
||||
_CurrentDataPath = _ComputerPath
|
||||
_Config = ReadFromFile(_ComputerPath)
|
||||
Else
|
||||
_Logger.Debug("Creating default config in UserPath: {0}", _UserPath)
|
||||
_CurrentDataPath = _UserPath
|
||||
_Config = Activator.CreateInstance(_Schema.GetType)
|
||||
|
||||
WriteToFile(_Config, _UserPath)
|
||||
If Not _File.TestPathIsDirectory(ComputerConfigPath) Then
|
||||
Throw New ArgumentException($"Path {ComputerConfigPath} is not a directory!")
|
||||
End If
|
||||
|
||||
_Config = LoadConfig()
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
@@ -81,6 +71,34 @@ Public Class ConfigManager(Of T)
|
||||
WriteToFile(_Config, _UserPath)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' First check if a user config exists and if it does, load it.
|
||||
''' If not, check if a systemwide config exists and and if it does, load it.
|
||||
''' Otherwise, create a user config using the default values from the supplied config class `T`
|
||||
''' </summary>
|
||||
''' <returns></returns>
|
||||
Private Function LoadConfig() As T
|
||||
Dim oConfig As T
|
||||
|
||||
If IO.File.Exists(_UserPath) Then
|
||||
_Logger.Debug("Loading config from UserPath: {0}", _UserPath)
|
||||
_CurrentDataPath = _UserPath
|
||||
oConfig = ReadFromFile(_UserPath)
|
||||
ElseIf IO.File.Exists(_ComputerPath) Then
|
||||
_Logger.Debug("Loading config from ComputerPath: {0}", _ComputerPath)
|
||||
_CurrentDataPath = _ComputerPath
|
||||
oConfig = ReadFromFile(_ComputerPath)
|
||||
Else
|
||||
_Logger.Debug("Creating default config in UserPath: {0}", _UserPath)
|
||||
_CurrentDataPath = _UserPath
|
||||
oConfig = Activator.CreateInstance(_Schema.GetType)
|
||||
|
||||
WriteToFile(_Config, _UserPath)
|
||||
End If
|
||||
|
||||
Return oConfig
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Serialize a config object to byte array
|
||||
''' </summary>
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
Imports System.IO
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Public MustInherit Class OldConfig
|
||||
Private Const _userConfigFileName As String = "UserConfig.xml"
|
||||
Private _logFactory As LogConfig
|
||||
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(LogConfig As LogConfig)
|
||||
_logFactory = LogConfig
|
||||
_logger = LogConfig.GetLogger()
|
||||
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user