diff --git a/Modules.Config/ConfigManager.vb b/Modules.Config/ConfigManager.vb index a082ca94..0017b6ae 100644 --- a/Modules.Config/ConfigManager.vb +++ b/Modules.Config/ConfigManager.vb @@ -14,7 +14,7 @@ Public Class ConfigManager(Of T) Private ReadOnly _UserPath As String Private ReadOnly _ComputerPath As String - Private _ForceUserConfig As Boolean = False + Private _UseUserConfig As Boolean = False Private _TestMode As Boolean = False ''' @@ -23,6 +23,11 @@ Public Class ConfigManager(Of T) Private ReadOnly _Blueprint As T Private ReadOnly _Serializer As XmlSerializer + Private ReadOnly _ExcludedAttributes = New List(Of Type) From { + GetType(ConnectionStringAttribute), + GetType(ConnectionStringTestAttribute) + } + Public ReadOnly Property Config As T ''' @@ -40,7 +45,7 @@ Public Class ConfigManager(Of T) _UserPath = Path.Combine(UserConfigPath, USER_CONFIG_NAME) _ComputerPath = Path.Combine(ComputerConfigPath, COMPUTER_CONFIG_NAME) - _ForceUserConfig = ForceUserConfig + _UseUserConfig = ForceUserConfig _Blueprint = Activator.CreateInstance(Of T) _Serializer = New XmlSerializer(_Blueprint.GetType) @@ -66,6 +71,8 @@ Public Class ConfigManager(Of T) End If _Config = LoadConfig() + + End Sub ''' @@ -80,10 +87,11 @@ Public Class ConfigManager(Of T) ''' ''' Save the current config object to `UserConfigPath` ''' + ''' Force saving all attributes including the attributes marked as excluded ''' True if save was successful, False otherwise - Public Function Save() As Boolean + Public Function Save(Optional ForceAll As Boolean = False) As Boolean Try - WriteToFile(_Config, _UserPath) + WriteToFile(_Config, _UserPath, ForceAll) Return True Catch ex As Exception _Logger.Error(ex) @@ -121,6 +129,20 @@ Public Class ConfigManager(Of T) Next End Sub + ''' + ''' Filters a config object by copying all values except `ExcludedAttributeTypes` + ''' + ''' Config Class + ''' Config object + ''' List of Attribute type to exclude + ''' + Private Function FilterValues(Of T)(ByVal Data As T, ExcludedAttributeTypes As List(Of Type)) As T + Dim oResult As T = Activator.CreateInstance(Of T) + + CopyValues(Data, oResult, ExcludedAttributeTypes) + Return oResult + End Function + Private Function LoadConfig() As T ' first create an empty/default config object Dim oConfig = Activator.CreateInstance(_Blueprint.GetType) @@ -141,7 +163,7 @@ Public Class ConfigManager(Of T) _Logger.Warn("Computer config could not be loaded!") End Try Else - _ForceUserConfig = True + _UseUserConfig = True End If Return Config @@ -157,13 +179,10 @@ Public Class ConfigManager(Of T) Dim oExcludedAttributes As New List(Of Type) ' Copy values from user config to final config - If _ForceUserConfig Then + If _UseUserConfig Then CopyValues(oUserConfig, Config, New List(Of Type)) Else - CopyValues(oUserConfig, Config, New List(Of Type) From { - GetType(ConnectionStringAttribute), - GetType(ConnectionStringTestAttribute) - }) + CopyValues(oUserConfig, Config, _ExcludedAttributes) End If End If @@ -177,19 +196,6 @@ Public Class ConfigManager(Of T) Return Config End Function - Private Function LoadDefaultConfig() As T - _Logger.Debug("Creating default config in UserPath: {0}", _UserPath) - Dim oConfig = Activator.CreateInstance(_Blueprint.GetType) - - Try - WriteToFile(oConfig, _UserPath) - Catch ex As Exception - _Logger.Warn("Could not create default config in UserPath: {0}", _UserPath) - End Try - - Return oConfig - End Function - Private Function TestHasAttribute(Config As T, AttributeType As Type) As Boolean For Each oProperty As PropertyInfo In Config.GetType.GetProperties() If Attribute.IsDefined(oProperty, GetType(ConnectionStringAttribute)) Then @@ -224,9 +230,16 @@ Public Class ConfigManager(Of T) ''' ''' The object to write ''' The file name to write to - Private Sub WriteToFile(Data As T, Path As String) + Private Sub WriteToFile(Data As T, Path As String, ForceAll As Boolean) Try _Logger.Debug("Saving config to: {0}", Path) + + ' If config was loaded from computer config, + ' DO NOT save connection string, etc. to user config + If _UseUserConfig = False And ForceAll = False Then + Data = FilterValues(Data, _ExcludedAttributes) + End If + Dim oBytes = Serialize(Data) Using oFileStream = New FileStream(Path, FileMode.Create, FileAccess.Write) diff --git a/Modules.Config/My Project/AssemblyInfo.vb b/Modules.Config/My Project/AssemblyInfo.vb index 7a591066..b70e1b50 100644 --- a/Modules.Config/My Project/AssemblyInfo.vb +++ b/Modules.Config/My Project/AssemblyInfo.vb @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices ' übernehmen, indem Sie "*" eingeben: ' - +