WIP: Use ConfigManager

This commit is contained in:
Jonathan Jenne
2019-05-28 14:07:35 +02:00
parent e9135d3543
commit 2bd7ff9d2c
17 changed files with 418 additions and 248 deletions

View File

@@ -15,217 +15,239 @@ Module ModuleMySettings
Public FW_started As Boolean = False
Public FWSCAN_started As Boolean = False
Public Function GetUserConfigPath() As String
Return Path.Combine(Application.UserAppDataPath(), USER_CONFIG_FILE)
End Function
'Public Function GetUserConfigPath() As String
' Return Path.Combine(Application.UserAppDataPath(), USER_CONFIG_FILE)
'End Function
Public Function GetAllUsersConfigPath() As String
Return Path.Combine(Application.CommonAppDataPath(), COMPUTER_CONFIG_FILE)
End Function
'Public Function GetAllUsersConfigPath() As String
' Return Path.Combine(Application.CommonAppDataPath(), COMPUTER_CONFIG_FILE)
'End Function
Public Function GetCurrentConfigPath() As String
If File.Exists(GetUserConfigPath()) Then
Return GetUserConfigPath()
Else
Return GetAllUsersConfigPath()
End If
End Function
'Public Function GetCurrentConfigPath() As String
' If File.Exists(GetUserConfigPath()) Then
' Return GetUserConfigPath()
' Else
' Return GetAllUsersConfigPath()
' End If
'End Function
Public Function LoadMyConfig()
Dim rowresult As String = ""
Try
Dim oDatatable As DataTable
''if file doesn't exist, create the file with its default xml table
'If Not File.Exists(ConfigPath) Then
' DT = CreateConfigTable()
' DT.WriteXml(ConfigPath)
'End If
'DT = GetTablefromXML()
'Public Function LoadMyConfig()
' Dim rowresult As String = ""
' Try
' Dim oDatatable As DataTable
' ''if file doesn't exist, create the file with its default xml table
' 'If Not File.Exists(ConfigPath) Then
' ' DT = CreateConfigTable()
' ' DT.WriteXml(ConfigPath)
' 'End If
' 'DT = GetTablefromXML()
' if file in %APPDATA% doesn't exist,
' check for file in %ALLUSERSPROFILE%,
' otherwise create the file with its default xml table
If File.Exists(GetUserConfigPath()) Then
oDatatable = GetTablefromXML(GetUserConfigPath())
ElseIf File.Exists(GetAllUsersConfigPath()) Then
oDatatable = GetTablefromXML(GetAllUsersConfigPath())
Else
oDatatable = CreateConfigTable()
oDatatable.WriteXml(GetUserConfigPath())
End If
' ' if file in %APPDATA% doesn't exist,
' ' check for file in %ALLUSERSPROFILE%,
' ' otherwise create the file with its default xml table
' If File.Exists(GetUserConfigPath()) Then
' oDatatable = GetTablefromXML(GetUserConfigPath())
' ElseIf File.Exists(GetAllUsersConfigPath()) Then
' oDatatable = GetTablefromXML(GetAllUsersConfigPath())
' Else
' oDatatable = CreateConfigTable()
' oDatatable.WriteXml(GetUserConfigPath())
' End If
For Each Row As DataRow In oDatatable.Rows
rowresult &= Row.Item("ConfigName")
Select Case Row.Item("ConfigName")
Case "MyConnectionString"
Dim connstring As String
'Den ConnectonString mit verschlüsseltem PW laden
Dim csb As New SqlClient.SqlConnectionStringBuilder
csb.ConnectionString = Row.Item("Value")
If csb.ConnectionString.Contains("Password=") Then
'SA-Auth
'Jetzt das Passwort entschlüsseln
Dim PWplainText As String
Dim wrapper As New ClassEncryption("!35452didalog=")
' DecryptData throws if the wrong password is used.
Try
PWplainText = wrapper.DecryptData(csb.Password)
Catch ex As Exception
ClassLogger.Add("- the Password '" & csb.Password & "' could not be decrypted", False)
PWplainText = csb.Password
End Try
connstring = Row.Item("Value").ToString.Replace(csb.Password, PWplainText)
Else
'Win-Auth
connstring = Row.Item("Value").ToString
End If
' For Each Row As DataRow In oDatatable.Rows
' rowresult &= Row.Item("ConfigName")
' Select Case Row.Item("ConfigName")
' Case "MyConnectionString"
' Dim connstring As String
' 'Den ConnectonString mit verschlüsseltem PW laden
' Dim csb As New SqlClient.SqlConnectionStringBuilder
' csb.ConnectionString = Row.Item("Value")
' If csb.ConnectionString.Contains("Password=") Then
' 'SA-Auth
' 'Jetzt das Passwort entschlüsseln
' Dim PWplainText As String
' Dim wrapper As New ClassEncryption("!35452didalog=")
' ' DecryptData throws if the wrong password is used.
' Try
' PWplainText = wrapper.DecryptData(csb.Password)
' Catch ex As Exception
' ClassLogger.Add("- the Password '" & csb.Password & "' could not be decrypted", False)
' PWplainText = csb.Password
' End Try
' connstring = Row.Item("Value").ToString.Replace(csb.Password, PWplainText)
' Else
' 'Win-Auth
' connstring = Row.Item("Value").ToString
' End If
MyConnectionString = connstring
Case "LogErrorsOnly"
LogErrorsOnly = CBool(Row.Item("Value"))
Case "Preview"
Preview = CBool(Row.Item("Value"))
Case "UniversalViewer"
UniversalViewer_Path = Row.Item("Value")
Case "FW_started"
FW_started = CBool(Row.Item("Value"))
Case "FWSCAN_started"
FWSCAN_started = CBool(Row.Item("Value"))
Case "Delete_OriginFile"
CURR_DELETE_ORIGIN = CBool(Row.Item("Value"))
End Select
' MyConnectionString = connstring
' Case "LogErrorsOnly"
' LogErrorsOnly = CBool(Row.Item("Value"))
' Case "Preview"
' Preview = CBool(Row.Item("Value"))
' Case "UniversalViewer"
' UniversalViewer_Path = Row.Item("Value")
' Case "FW_started"
' FW_started = CBool(Row.Item("Value"))
' Case "FWSCAN_started"
' FWSCAN_started = CBool(Row.Item("Value"))
' Case "Delete_OriginFile"
' CURR_DELETE_ORIGIN = CBool(Row.Item("Value"))
' End Select
Next
'update 1.1
If rowresult.Contains("FW_started") = False Then
Dim newRow As DataRow = oDatatable.NewRow()
newRow("ConfigName") = "FW_started"
newRow("Value") = "False"
oDatatable.Rows.Add(newRow)
oDatatable.WriteXml(GetUserConfigPath())
End If
'update 1.6
If rowresult.Contains("Delete_OriginFile") = False Then
Dim newRow As DataRow = oDatatable.NewRow()
newRow("ConfigName") = "Delete_OriginFile"
newRow("Value") = "False"
oDatatable.Rows.Add(newRow)
oDatatable.WriteXml(GetUserConfigPath())
End If
If rowresult.Contains("FWSCAN_started") = False Then
Dim newRow As DataRow = oDatatable.NewRow()
newRow("ConfigName") = "FWSCAN_started"
newRow("Value") = "False"
oDatatable.Rows.Add(newRow)
oDatatable.WriteXml(GetUserConfigPath())
End If
Catch ex As Exception
MsgBox("Error in MySettings-LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
Return True
' Next
' 'update 1.1
' If rowresult.Contains("FW_started") = False Then
' Dim newRow As DataRow = oDatatable.NewRow()
' newRow("ConfigName") = "FW_started"
' newRow("Value") = "False"
' oDatatable.Rows.Add(newRow)
' oDatatable.WriteXml(GetUserConfigPath())
' End If
' 'update 1.6
' If rowresult.Contains("Delete_OriginFile") = False Then
' Dim newRow As DataRow = oDatatable.NewRow()
' newRow("ConfigName") = "Delete_OriginFile"
' newRow("Value") = "False"
' oDatatable.Rows.Add(newRow)
' oDatatable.WriteXml(GetUserConfigPath())
' End If
' If rowresult.Contains("FWSCAN_started") = False Then
' Dim newRow As DataRow = oDatatable.NewRow()
' newRow("ConfigName") = "FWSCAN_started"
' newRow("Value") = "False"
' oDatatable.Rows.Add(newRow)
' oDatatable.WriteXml(GetUserConfigPath())
' End If
' Catch ex As Exception
' MsgBox("Error in MySettings-LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
' Return False
' End Try
' Return True
End Function
Private Function GetTablefromXML(ConfigPath As String)
Try
Dim DS As New DataSet
DS.ReadXml(ConfigPath)
Return DS.Tables(0)
Catch ex As Exception
MsgBox("Error in GetTablefromXML" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return Nothing
End Try
'End Function
'Private Function GetTablefromXML(ConfigPath As String)
' Try
' Dim DS As New DataSet
' DS.ReadXml(ConfigPath)
' Return DS.Tables(0)
' Catch ex As Exception
' MsgBox("Error in GetTablefromXML" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
' Return Nothing
' End Try
End Function
Private Function CreateConfigTable() As DataTable
Try
' Create sample Customers table, in order
' to demonstrate the behavior of the DataTableReader.
Dim oTable As New DataTable
oTable.TableName = "MyConfig"
'End Function
'Private Function CreateConfigTable() As DataTable
' Try
' ' Create sample Customers table, in order
' ' to demonstrate the behavior of the DataTableReader.
' Dim oTable As New DataTable
' oTable.TableName = "MyConfig"
' Create two columns, ID and Name.
Dim oIdColumn As DataColumn = oTable.Columns.Add("ID",
GetType(System.Int32))
' ' Create two columns, ID and Name.
' Dim oIdColumn As DataColumn = oTable.Columns.Add("ID",
' GetType(System.Int32))
oIdColumn.AutoIncrement = True
oIdColumn.AutoIncrementSeed = 0
oIdColumn.AutoIncrementStep = 1
oTable.Columns.Add("ConfigName", GetType(System.String))
oTable.Columns.Add("Value", GetType(System.String))
'Set the ID column as the primary key column.
oTable.PrimaryKey = New DataColumn() {oIdColumn}
Dim newRow As DataRow = oTable.NewRow()
newRow("ConfigName") = "MyConnectionString"
newRow("Value") = ""
oTable.Rows.Add(newRow)
Dim newRow1 As DataRow = oTable.NewRow()
newRow1("ConfigName") = "LogErrorsOnly"
newRow1("Value") = "True"
oTable.Rows.Add(newRow1)
Dim newRow2 As DataRow = oTable.NewRow()
newRow2("ConfigName") = "Preview"
newRow2("Value") = "True"
oTable.Rows.Add(newRow2)
Dim newRow3 As DataRow = oTable.NewRow()
newRow3("ConfigName") = "UniversalViewer"
newRow3("Value") = ""
oTable.Rows.Add(newRow3)
Dim newRow4 As DataRow = oTable.NewRow()
newRow4("ConfigName") = "FW_started"
newRow4("Value") = "False"
oTable.Rows.Add(newRow4)
Dim newRow5 As DataRow = oTable.NewRow()
newRow5("ConfigName") = "FWSCAN_started"
newRow5("Value") = "False"
oTable.Rows.Add(newRow5)
oTable.AcceptChanges()
Return oTable
Catch ex As Exception
MsgBox("Error in CreateConfigTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return Nothing
End Try
End Function
' oIdColumn.AutoIncrement = True
' oIdColumn.AutoIncrementSeed = 0
' oIdColumn.AutoIncrementStep = 1
' oTable.Columns.Add("ConfigName", GetType(System.String))
' oTable.Columns.Add("Value", GetType(System.String))
' 'Set the ID column as the primary key column.
' oTable.PrimaryKey = New DataColumn() {oIdColumn}
' Dim newRow As DataRow = oTable.NewRow()
' newRow("ConfigName") = "MyConnectionString"
' newRow("Value") = ""
' oTable.Rows.Add(newRow)
' Dim newRow1 As DataRow = oTable.NewRow()
' newRow1("ConfigName") = "LogErrorsOnly"
' newRow1("Value") = "True"
' oTable.Rows.Add(newRow1)
' Dim newRow2 As DataRow = oTable.NewRow()
' newRow2("ConfigName") = "Preview"
' newRow2("Value") = "True"
' oTable.Rows.Add(newRow2)
' Dim newRow3 As DataRow = oTable.NewRow()
' newRow3("ConfigName") = "UniversalViewer"
' newRow3("Value") = ""
' oTable.Rows.Add(newRow3)
' Dim newRow4 As DataRow = oTable.NewRow()
' newRow4("ConfigName") = "FW_started"
' newRow4("Value") = "False"
' oTable.Rows.Add(newRow4)
' Dim newRow5 As DataRow = oTable.NewRow()
' newRow5("ConfigName") = "FWSCAN_started"
' newRow5("Value") = "False"
' oTable.Rows.Add(newRow5)
' oTable.AcceptChanges()
' Return oTable
' Catch ex As Exception
' MsgBox("Error in CreateConfigTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
' Return Nothing
' End Try
'End Function
''' <summary>
''' Save settings to user config, NOT to common config
''' </summary>
Public Function SaveConfigValue(name As String, value As String)
Try
Dim oUserConfigPath = GetUserConfigPath()
Dim oCurrentConfigPath = GetCurrentConfigPath()
'''' <summary>
'''' Save settings to user config, NOT to common config
'''' </summary>
'Public Function SaveConfigValue(name As String, value As String)
' Try
' Dim oUserConfigPath = GetUserConfigPath()
' Dim oCurrentConfigPath = GetCurrentConfigPath()
Dim oDatatable As DataTable = GetTablefromXML(oCurrentConfigPath)
' Dim oDatatable As DataTable = GetTablefromXML(oCurrentConfigPath)
For Each Row As DataRow In oDatatable.Rows
If Row.Item("ConfigName") = name Then
Row.Item("Value") = value
End If
Next
oDatatable.AcceptChanges()
oDatatable.WriteXml(oUserConfigPath)
' For Each Row As DataRow In oDatatable.Rows
' If Row.Item("ConfigName") = name Then
' Row.Item("Value") = value
' End If
' Next
' oDatatable.AcceptChanges()
' oDatatable.WriteXml(oUserConfigPath)
Return True
Catch ex As Exception
MsgBox("Error in SaveConfigValue" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
End Function
Public Function Load_BasicConfig()
Try
ClassDatabase.Init()
Dim sql As String = "select * from tbdd_Modules where NAME = 'Global-Indexer'"
Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
If DT.Rows.Count = 1 Then
GI_withWindream = DT.Rows(0).Item("BIT1")
vWLaufwerk = DT.Rows(0).Item("STRING1")
Else
Return False
End If
Catch ex As Exception
MsgBox("Error in Load_BasicConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
Return True
End Function
' Return True
' Catch ex As Exception
' MsgBox("Error in SaveConfigValue" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
' Return False
' End Try
'End Function
'Public Function Load_BasicConfig()
' Try
' ClassDatabase.Init()
' Dim sql As String = "select * from tbdd_Modules where NAME = 'Global-Indexer'"
' Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
' If DT.Rows.Count = 1 Then
' GI_withWindream = DT.Rows(0).Item("BIT1")
' vWLaufwerk = DT.Rows(0).Item("STRING1")
' Else
' Return False
' End If
' Catch ex As Exception
' MsgBox("Error in Load_BasicConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
' Return False
' End Try
' Return True
'End Function
'Private Function DecryptConnectionString(EncryptedConnectionString As String) As String
' Dim oBuilder As New SqlClient.SqlConnectionStringBuilder With {
' .ConnectionString = EncryptedConnectionString
' }
' If oBuilder.ConnectionString.Contains("Password=") Then
' Dim oPlaintextPassword As String
' Dim oDecryptor As New ClassEncryption("!35452didalog=")
' Try
' oPlaintextPassword = oDecryptor.DecryptData(oBuilder.Password)
' Catch ex As Exception
' LOGGER.Error(ex)
' LOGGER.Debug("Password {0} could not be decrypted. Assuming plaintext password.")
' oPlaintextPassword = oBuilder.Password
' End Try
' Return EncryptedConnectionString.Replace(oBuilder.Password, oPlaintextPassword)
' Else
' Return EncryptedConnectionString
' End If
'End Function
End Module