2018-06-29 16:43:15 +02:00

161 lines
6.4 KiB
VB.net

Imports System.IO
Imports DD_LIB_Standards
Module ModuleRuntime
Public MyConnectionString As String
Public LogErrorsOnly As Boolean = True
Public ConfigPath As String = Path.Combine(Application.UserAppDataPath, "UserConfig.xml")
Public ActiveDirectoryRootNode As String = $"LDAP://{Environment.UserDomainName}"
Public Function SaveMySettingsValue(name As String, value As String, type As String)
Try
Dim DT As DataTable
If type = "ConfigMain" Then
DT = GetTablefromXML(ConfigPath)
End If
If Not DT Is Nothing Then
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)
Else
MsgBox("Setting could not be saved! Check logfile.", MsgBoxStyle.Critical)
End If
Catch ex As Exception
MsgBox("Error in SaveConfigValue" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
Return True
End Function
Private Function GetTablefromXML(path As String)
Try
Dim DS As New DataSet
DS.ReadXml(path)
Return DS.Tables(0)
Catch ex As Exception
MsgBox("Error in GetTablefromXML" & vbNewLine & ex.Message & vbNewLine & "ConfigPath: " & vbNewLine & path, MsgBoxStyle.Critical)
clsLogger.Add("Error in GetTablefromXML: " & ex.Message, True)
clsLogger.Add(">> ConfigPath: " & ConfigPath, False)
Return Nothing
End Try
End Function
Public Function MySettings_Load()
Try
Dim DT As DataTable
'if file doesn't exist, create the file with its default xml table
If Not File.Exists(ConfigPath) Then
clsLogger.Add(">> ConfigFile was created in: " & ConfigPath, False)
DT = CreateConfigTable()
DT.WriteXml(ConfigPath)
clsLogger.Add(">> Defaultvalues were saved.", False)
End If
DT = GetTablefromXML(ConfigPath)
If DT Is Nothing Then
MsgBox("Configuration could not be loaded!! Check LogFile!", MsgBoxStyle.Critical)
Return False
End If
For Each Row As DataRow In DT.Rows
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 Not csb.ConnectionString = "" Then
If csb.ConnectionString.Contains("Password=") Then
'sa-
'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)
connstring = Row.Item("Value").ToString.Replace(csb.Password, PWplainText)
Catch ex As Exception
clsLogger.Add("- the Password '" & csb.Password & "' could not be decrypted", False)
connstring = ""
End Try
Else
'Windows-Auth
connstring = Row.Item("Value").ToString
End If
MyConnectionString = connstring
Else
MyConnectionString = ""
End If
Case "LogErrorsOnly"
LogErrorsOnly = CBool(Row.Item("Value"))
Case "ActiveDirectoryRootNode"
Dim rootNode As String = Row.Item("Value")
If rootNode <> String.Empty Then
ActiveDirectoryRootNode = rootNode
End If
End Select
Next
Catch ex As Exception
MsgBox("Error in LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
clsLogger.Add("Error in LoadMyConfig: " & ex.Message, True)
Return False
End Try
Return True
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
newRow = table.NewRow()
newRow.Item("ConfigName") = "MyConnectionString"
newRow.Item("Value") = ""
table.Rows.Add(newRow)
newRow = table.NewRow()
newRow.Item("ConfigName") = "LogErrorsOnly"
newRow.Item("Value") = "True"
table.Rows.Add(newRow)
newRow = table.NewRow()
newRow.Item("ConfigName") = "ActiveDirectoryRootNode"
newRow.Item("Value") = ""
table.Rows.Add(newRow)
table.AcceptChanges()
clsLogger.Add(">> CreateConfigTable su...", False)
Return table
Catch ex As Exception
MsgBox("Error in CreateConfigTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return Nothing
End Try
End Function
End Module