jj: check for config in programdata

This commit is contained in:
Jonathan Jenne 2019-02-11 10:51:18 +01:00
parent d2e817b1e0
commit f4de7df9b5
2 changed files with 95 additions and 78 deletions

View File

@ -2,7 +2,9 @@
Imports System.Xml Imports System.Xml
Module ModuleMySettings Module ModuleMySettings
Dim ConfigPath As String = Path.Combine(Application.UserAppDataPath(), "UserConfig.xml") 'Dim ConfigPath As String = Path.Combine(Application.UserAppDataPath(), "UserConfig.xml")
Public Const CONFIG_FILE = "UserConfig.xml"
Public MyConnectionString As String = "" Public MyConnectionString As String = ""
Public UniversalViewer As String = "" Public UniversalViewer As String = ""
Public PDFXChangeViewer As String = "" Public PDFXChangeViewer As String = ""
@ -23,173 +25,188 @@ Module ModuleMySettings
Public vFILE_DELIMITER As String = "_" Public vFILE_DELIMITER As String = "_"
Public WMSESSION_STARTSTOP_STARTUP As Boolean = False Public WMSESSION_STARTSTOP_STARTUP As Boolean = False
Public Function GetUserConfigPath() As String
Return Path.Combine(Application.UserAppDataPath(), CONFIG_FILE)
End Function
Public Function GetAllUsersConfigPath() As String
Return Path.Combine(Application.CommonAppDataPath(), CONFIG_FILE)
End Function
Public Function Settings_Load() Public Function Settings_Load()
Try Try
Dim DT As DataTable Dim oDatatable As DataTable
'if file doesn't exist, create the file with its default xml table ' if file in %APPDATA% doesn't exist,
If Not File.Exists(ConfigPath) Then ' check for file in %ALLUSERSPROFILE%,
DT = CreateConfigTable() ' otherwise create the file with its default xml table
DT.WriteXml(ConfigPath) If File.Exists(GetUserConfigPath()) Then
oDatatable = GetTablefromXML(GetUserConfigPath())
ElseIf File.Exists(GetAllUsersConfigPath()) Then
oDatatable = GetTablefromXML(GetAllUsersConfigPath())
Else
oDatatable = CreateConfigTable()
oDatatable.WriteXml(GetUserConfigPath())
End If End If
DT = GetTablefromXML()
For Each Row As DataRow In DT.Rows For Each oRow As DataRow In oDatatable.Rows
Select Case Row.Item("ConfigName") Select Case oRow.Item("ConfigName")
Case "MyConnectionString" Case "MyConnectionString"
Dim connstring As String Dim oConnectionString As String
'Den ConnectonString mit verschlüsseltem PW laden 'Den ConnectonString mit verschlüsseltem PW laden
Dim csb As New SqlClient.SqlConnectionStringBuilder Dim oBuilder As New SqlClient.SqlConnectionStringBuilder
csb.ConnectionString = Row.Item("Value") oBuilder.ConnectionString = oRow.Item("Value")
If Not csb.ConnectionString = "" Then If Not oBuilder.ConnectionString = "" Then
If csb.ConnectionString.Contains("Password=") Then If oBuilder.ConnectionString.Contains("Password=") Then
'sa- 'sa-
'Jetzt das Passwort entschlüsseln 'Jetzt das Passwort entschlüsseln
Dim PWplainText As String Dim PWplainText As String
Dim wrapper As New ClassEncryption("!35452didalog=") Dim wrapper As New ClassEncryption("!35452didalog=")
' DecryptData throws if the wrong password is used. ' DecryptData throws if the wrong password is used.
Try Try
PWplainText = wrapper.DecryptData(csb.Password) PWplainText = wrapper.DecryptData(oBuilder.Password)
Catch ex As Exception Catch ex As Exception
ClassLogger.Add("- the Password '" & csb.Password & "' could not be decrypted", False) ClassLogger.Add("- the Password '" & oBuilder.Password & "' could not be decrypted", False)
PWplainText = csb.Password PWplainText = oBuilder.Password
End Try End Try
connstring = Row.Item("Value").ToString.Replace(csb.Password, PWplainText) oConnectionString = oRow.Item("Value").ToString.Replace(oBuilder.Password, PWplainText)
Else Else
'Windows-Auth 'Windows-Auth
connstring = Row.Item("Value").ToString oConnectionString = oRow.Item("Value").ToString
End If End If
MyConnectionString = connstring MyConnectionString = oConnectionString
ClassDatabase.Init() ClassDatabase.Init()
Else Else
MyConnectionString = "" MyConnectionString = ""
End If End If
Case "LogErrorsOnly" Case "LogErrorsOnly"
LogErrorsOnly = CBool(Row.Item("Value")) LogErrorsOnly = CBool(oRow.Item("Value"))
Case "UniversalViewer" Case "UniversalViewer"
UniversalViewer = Row.Item("Value") UniversalViewer = oRow.Item("Value")
Case "PDFXChangeViewer" Case "PDFXChangeViewer"
PDFXChangeViewer = Row.Item("Value") PDFXChangeViewer = oRow.Item("Value")
Case "PDFViewer_ZoomMode" Case "PDFViewer_ZoomMode"
PDFViewer_ZoomMode = Row.Item("Value") PDFViewer_ZoomMode = oRow.Item("Value")
Case "vpdfviewer" Case "vpdfviewer"
vpdfviewer = Row.Item("Value") vpdfviewer = oRow.Item("Value")
Case "Viewer" Case "Viewer"
Viewer = Row.Item("Value") Viewer = oRow.Item("Value")
Case "SumatraViewer" Case "SumatraViewer"
SumatraViewer = Row.Item("Value") SumatraViewer = oRow.Item("Value")
Case "IDX_DMS_ERSTELLT" Case "IDX_DMS_ERSTELLT"
IDX_DMS_ERSTELLT = Row.Item("Value") IDX_DMS_ERSTELLT = oRow.Item("Value")
Case "IDX_DMS_ERSTELLT_ZEIT" Case "IDX_DMS_ERSTELLT_ZEIT"
IDX_DMS_ERSTELLT_ZEIT = Row.Item("Value") IDX_DMS_ERSTELLT_ZEIT = oRow.Item("Value")
Case "USRMNGRPATH" Case "USRMNGRPATH"
USRMNGRPATH = Row.Item("Value") USRMNGRPATH = oRow.Item("Value")
End Select End Select
Next Next
Return True
Catch ex As Exception Catch ex As Exception
MsgBox("Error in LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical) MsgBox("Error in LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False Return False
End Try End Try
Return True
End Function End Function
Private Function GetTablefromXML() Private Function GetTablefromXML(ConfigPath As String)
Try Try
Dim DS As New DataSet Dim oDataset As New DataSet
DS.ReadXml(ConfigPath) oDataset.ReadXml(ConfigPath)
Return DS.Tables(0) Return oDataset.Tables(0)
Catch ex As Exception Catch ex As Exception
MsgBox("Error in GetTablefromXML" & vbNewLine & ex.Message, MsgBoxStyle.Critical) MsgBox("Error in GetTablefromXML" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return Nothing Return Nothing
End Try End Try
End Function End Function
Private Function CreateConfigTable() As DataTable Private Function CreateConfigTable() As DataTable
Try Try
' Create sample Customers table, in order ' Create sample Customers table, in order
' to demonstrate the behavior of the DataTableReader. ' to demonstrate the behavior of the DataTableReader.
Dim table As New DataTable Dim oTable As New DataTable
table.TableName = "MyConfig" oTable.TableName = "MyConfig"
' Create two columns, ID and Name. ' Create two columns, ID and Name.
Dim idColumn As DataColumn = table.Columns.Add("ID", _ Dim oIdColumn As DataColumn = oTable.Columns.Add("ID",
GetType(System.Int32)) GetType(System.Int32))
idColumn.AutoIncrement = True oIdColumn.AutoIncrement = True
idColumn.AutoIncrementSeed = 0 oIdColumn.AutoIncrementSeed = 0
idColumn.AutoIncrementStep = 1 oIdColumn.AutoIncrementStep = 1
table.Columns.Add("ConfigName", GetType(System.String)) oTable.Columns.Add("ConfigName", GetType(System.String))
table.Columns.Add("Value", GetType(System.String)) oTable.Columns.Add("Value", GetType(System.String))
'Set the ID column as the primary key column. 'Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn} oTable.PrimaryKey = New DataColumn() {oIdColumn}
Dim newRow As DataRow = table.NewRow() Dim newRow As DataRow = oTable.NewRow()
newRow("ConfigName") = "MyConnectionString" newRow("ConfigName") = "MyConnectionString"
newRow("Value") = "" newRow("Value") = ""
table.Rows.Add(newRow) oTable.Rows.Add(newRow)
Dim newRow1 As DataRow = table.NewRow() Dim newRow1 As DataRow = oTable.NewRow()
newRow1("ConfigName") = "LogErrorsOnly" newRow1("ConfigName") = "LogErrorsOnly"
newRow1("Value") = "True" newRow1("Value") = "True"
table.Rows.Add(newRow1) oTable.Rows.Add(newRow1)
Dim newRow2 As DataRow = table.NewRow() Dim newRow2 As DataRow = oTable.NewRow()
newRow2("ConfigName") = "UniversalViewer" newRow2("ConfigName") = "UniversalViewer"
newRow2("Value") = "" newRow2("Value") = ""
table.Rows.Add(newRow2) oTable.Rows.Add(newRow2)
Dim newRow3 As DataRow = table.NewRow() Dim newRow3 As DataRow = oTable.NewRow()
newRow3("ConfigName") = "PDFXChangeViewer" newRow3("ConfigName") = "PDFXChangeViewer"
newRow3("Value") = "" newRow3("Value") = ""
table.Rows.Add(newRow3) oTable.Rows.Add(newRow3)
Dim newRow7 As DataRow = table.NewRow() Dim newRow7 As DataRow = oTable.NewRow()
newRow7("ConfigName") = "SumatraViewer" newRow7("ConfigName") = "SumatraViewer"
newRow7("Value") = "" newRow7("Value") = ""
table.Rows.Add(newRow7) oTable.Rows.Add(newRow7)
Dim newRow4 As DataRow = table.NewRow() Dim newRow4 As DataRow = oTable.NewRow()
newRow4("ConfigName") = "PDFViewer_ZoomMode" newRow4("ConfigName") = "PDFViewer_ZoomMode"
newRow4("Value") = "3" newRow4("Value") = "3"
table.Rows.Add(newRow4) oTable.Rows.Add(newRow4)
Dim newRow5 As DataRow = table.NewRow() Dim newRow5 As DataRow = oTable.NewRow()
newRow5("ConfigName") = "vpdfviewer" newRow5("ConfigName") = "vpdfviewer"
newRow5("Value") = "internal" newRow5("Value") = "internal"
table.Rows.Add(newRow5) oTable.Rows.Add(newRow5)
Dim newRow6 As DataRow = table.NewRow() Dim newRow6 As DataRow = oTable.NewRow()
newRow6("ConfigName") = "Viewer" newRow6("ConfigName") = "Viewer"
newRow6("Value") = "docview" newRow6("Value") = "docview"
table.Rows.Add(newRow6) oTable.Rows.Add(newRow6)
Dim newRow8 As DataRow = table.NewRow() Dim newRow8 As DataRow = oTable.NewRow()
newRow8("ConfigName") = "IDX_DMS_ERSTELLT" newRow8("ConfigName") = "IDX_DMS_ERSTELLT"
newRow8("Value") = "DMS erstellt" newRow8("Value") = "DMS erstellt"
table.Rows.Add(newRow8) oTable.Rows.Add(newRow8)
Dim newRow9 As DataRow = table.NewRow() Dim newRow9 As DataRow = oTable.NewRow()
newRow9("ConfigName") = "IDX_DMS_ERSTELLT_ZEIT" newRow9("ConfigName") = "IDX_DMS_ERSTELLT_ZEIT"
newRow9("Value") = "DMS erstellt (Zeit)" newRow9("Value") = "DMS erstellt (Zeit)"
table.Rows.Add(newRow9) oTable.Rows.Add(newRow9)
Dim newRow10 As DataRow = table.NewRow() Dim newRow10 As DataRow = oTable.NewRow()
newRow10("ConfigName") = "USRMNGRPATH" newRow10("ConfigName") = "USRMNGRPATH"
newRow10("Value") = "" newRow10("Value") = ""
table.Rows.Add(newRow10) oTable.Rows.Add(newRow10)
table.AcceptChanges() oTable.AcceptChanges()
Return table Return oTable
Catch ex As Exception Catch ex As Exception
MsgBox("Error in CreateConfigTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical) MsgBox("Error in CreateConfigTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return Nothing Return Nothing
End Try End Try
End Function End Function
''' <summary>
''' Save settings to user config, NOT to common config
''' </summary>
Public Function SaveMySettingsValue(name As String, value As String) Public Function SaveMySettingsValue(name As String, value As String)
Try Try
Dim DT As DataTable Dim oConfigPath = GetUserConfigPath()
DT = GetTablefromXML() Dim oDatatable As DataTable = GetTablefromXML(oConfigPath)
For Each Row As DataRow In DT.Rows For Each Row As DataRow In oDatatable.Rows
If Row.Item("ConfigName") = name Then If Row.Item("ConfigName") = name Then
Row.Item("Value") = value Row.Item("Value") = value
End If End If
Next Next
DT.AcceptChanges() oDatatable.AcceptChanges()
DT.WriteXml(ConfigPath) oDatatable.WriteXml(oConfigPath)
Return True
Catch ex As Exception Catch ex As Exception
MsgBox("Error in SaveConfigValue" & vbNewLine & ex.Message, MsgBoxStyle.Critical) MsgBox("Error in SaveConfigValue" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False Return False
End Try End Try
Return True
End Function End Function
End Module End Module

View File

@ -34,7 +34,7 @@ Public Class frmMain
Catch ex As Exception Catch ex As Exception
ClassLogger.Add("Error in Save FormLayout: " & ex.Message) ClassLogger.Add("Error in Save FormLayout: " & ex.Message)
End Try End Try
If WINDREAM.oSession.aLoggedin = True Then If WINDREAM?.oSession?.aLoggedin = True Then
WINDREAM.Stop_WMCC_andCo() WINDREAM.Stop_WMCC_andCo()
End If End If
'If _windreamPM.oSession.aLoggedin = True Then 'If _windreamPM.oSession.aLoggedin = True Then