2019-07-04 10:40:48 +02:00

143 lines
5.9 KiB
VB.net

Imports System.IO
Imports DD_LIB_Standards
Module modMySettings
Dim ConfigPath As String = Path.Combine(Application.UserAppDataPath(), "UserConfig.xml")
Public Function LoadMyConfig()
Dim rowresult As String = ""
Try
Dim DT 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()
For Each Row As DataRow In DT.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 clsEncryption("!35452didalog=")
' DecryptData throws if the wrong password is used.
Try
PWplainText = wrapper.DecryptData(csb.Password)
Catch ex As Exception
Logger.Info("- 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 "HotkeyFunctionKey"
HotkeyFunctionKey = Row.Item("Value")
Case "HotkeySearchKey"
HotkeySearchKey = Row.Item("Value")
End Select
Next
If rowresult.Contains("HotkeyFunctionKey") = False Then
Dim newRow As DataRow = DT.NewRow()
newRow("ConfigName") = "HotkeyFunctionKey"
newRow("Value") = "strg"
DT.Rows.Add(newRow)
DT.WriteXml(ConfigPath)
End If
If rowresult.Contains("HotkeySearchKey") = False Then
Dim newRow As DataRow = DT.NewRow()
newRow("ConfigName") = "HotkeySearchKey"
newRow("Value") = "f"
DT.Rows.Add(newRow)
DT.WriteXml(ConfigPath)
End If
Return True
Catch ex As Exception
MsgBox("Error in MySettings-LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
End Function
Private Function GetTablefromXML()
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 table As New DataTable
table.TableName = "MyConfig"
' Create two columns, ID and Name.
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(System.Int32))
idColumn.AutoIncrement = True
idColumn.AutoIncrementSeed = 0
idColumn.AutoIncrementStep = 1
table.Columns.Add("ConfigName", GetType(System.String))
table.Columns.Add("Value", GetType(System.String))
'Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
Dim newRow As DataRow = table.NewRow()
newRow("ConfigName") = "MyConnectionString"
newRow("Value") = ""
table.Rows.Add(newRow)
Dim newRow1 As DataRow = table.NewRow()
newRow1("ConfigName") = "LogErrorsOnly"
newRow1("Value") = "True"
table.Rows.Add(newRow1)
Dim newRow2 As DataRow = table.NewRow()
newRow2("ConfigName") = "HotkeyFunctionKey"
newRow2("Value") = "strg"
table.Rows.Add(newRow2)
Dim newRow3 As DataRow = table.NewRow()
newRow3("ConfigName") = "HotkeySearchKey"
newRow3("Value") = "f"
table.Rows.Add(newRow3)
table.AcceptChanges()
Return table
Catch ex As Exception
MsgBox("Error in CreateConfigTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return Nothing
End Try
End Function
Public Function SaveConfigValue(name As String, value As String)
Try
Dim DT As DataTable
DT = GetTablefromXML()
For Each Row As DataRow In DT.Rows
If Row.Item("ConfigName") = name Then
Row.Item("Value") = value
End If
Next
DT.AcceptChanges()
DT.WriteXml(ConfigPath)
Catch ex As Exception
MsgBox("Error in SaveConfigValue" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
Return True
End Function
End Module