PictureTagger/SB-PictureTagger/Modul_Tools.vb

314 lines
16 KiB
VB.net

Option Explicit On
Imports System.IO
Module Modul_Tools
'Methonden_Index:
'1.Datei- und Ordnerdialoge
' 1.1 FolderBrowserDialog <-- in Work (Main works)
' 1.2 OpenFileDialog <-- in Work (Don't work)
' 1.3 SaveFileDialog <-- in Work
'2.Config Load/Save
' 2.1 From INI
' 2.1.1 Config_INI_ReadValue
' 2.1.2 Config_INI_WriteValue
' ExampleConf = Config_INI_ReadValue(FilePath, Section, Key, [DefaultWert])
' [Error =] Config_INI_WriteValue(FilePath, Section, Key, Wert)
' 2.2 From My CMD seting Files
'3.PortableMode
' 3.1 PortableModeTrue
' Übergabe: ConfigFile Return: (PortableMode=True/False)
' 3.2 PortableMode
' Übergabe: ConfigFile [WorkDir] [AppName] Return: (PortableMode=True/False)
' ConfigFile Nahme und Extendion - Wird Indirekt Geändert zu KonfigPfad
' WorkDir Pfad des Arbeitsverzeichnises - Wird Indirekt Geändert
' AppName Erstellt einen Neuern Ordner mit Diesen Nahme, wenn Nicht Portabler Mode
'4.GPS_Tools
' 4.1 GPS_to_GMaps
'5.Ascii<->Base64
' 5.1 Ascii_To_Base64
' 5.2 Base64_To_Ascii
'###################################################################################################################
'############### 1.Datei- und Ordnerdialoge ########################################################################
'############### http://www.vb-fun.de/dotnet/tipps/tip0212.shtml ###################################################
'###################################################################################################################
'###################################################################################################################
'############### 1.1 FolderBrowserDialog ###########################################################################
'###################################################################################################################
Public Function FolderBrowserDialog(Optional ByVal sStartPath As String = "C:\", Optional ByVal ShowNewFolderButton As Boolean = True, Optional ByRef CheckError As Boolean = False) As String
'Verändert die übergebene variable,
'wenn der benutzer durch "OK" den neuen Pfad bestätigt
FolderBrowserDialog = ""
Using FolderBrowserD As New FolderBrowserDialog
With FolderBrowserD
' Ordnervorschlag
.SelectedPath = sStartPath
.Description = "Bitte einen Ordner auswählen."
'#Button "Neuen Ordner erstellen" anzeigen
.ShowNewFolderButton = ShowNewFolderButton
'#ruft Dialog auf, weitere Ausführung nur bei Resultat "OK"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
FolderBrowserDialog = .SelectedPath
Else
CheckError = True '#Ausführung bei Abruch
End If
End With 'FolderBrowserD
End Using
End Function 'FolderBrowserDialog
'###################################################################################################################
'############### 1.2 OpenFileDialog ################################################################################
'###################################################################################################################
'Private Sub Button1_Click(ByVal sender As Object, ByVal e As _
' EventArgs) Handles Button1.Click
' ' ### OpenFileDialog ###
' Dim txt As String = ""
' Using ofd As New OpenFileDialog
' With ofd
' ' Ordnervorschlag
' .InitialDirectory = Pfad
' .Title = "Wähle Datei zum öffnen"
' ' Dateivorschlag (falls sinnvoll)
' ' .FileName = "Datei.gif"
' ' Filter
' .Filter = TextBox2.Text
' ' boolsche Abfrage ob Mehrfachauswahl zulässig
' .Multiselect = Not ChkEinzel.Checked
' ' Zeile ruft den Dialog auf, weitere Ausführung nur bei
' ' Resultat OK:
' If .ShowDialog = Windows.Forms.DialogResult.OK Then
' If ChkEinzel.Checked = True Then
' ' Bei Einzelauswahl wertet man .FileName aus
' txt = .FileName & vbNewLine
' Else
' ' Bei möglicher Multiselektion wertet man
' ' das Array .FileNames aus
' For Each filename As String In .FileNames
' txt &= filename & vbNewLine
' Next
' End If
' ' Welcher Filter wurde verwendet?
' Dim idx As Integer = .FilterIndex
' txt &= "FilterIndex: " & idx.ToString & vbNewLine
' txt &= "Gewählter Filter: " & FilterDescription(.Filter, _
' idx) & vbNewLine
' txt &= "Multiselect: " & .Multiselect.ToString
' Else
' txt = "Abbruch durch Benutzer"
' End If
' End With
' End Using
' TextBox1.Text = txt
'End Sub 'OpenFileDialog
'###################################################################################################################
'############### 1.3 SaveFileDialog ################################################################################
'###################################################################################################################
'Private Sub Button2_Click(ByVal sender As Object, ByVal e As _
' EventArgs) Handles Button2.Click
' ' ### SaveFileDialog ###
' Dim txt As String
' Using sfd As New SaveFileDialog
' With sfd
' ' Ordnervorschlag
' .InitialDirectory = Pfad
' .Title = "Eingabe Datei zum speichern (Es wird hier nicht "_
' "wirklich überschrieben)"
' ' Dateivorschlag
' .FileName = "Datei.gif"
' ' Filter
' .Filter = TextBox2.Text
' ' Zeile ruft den Dialog auf, weitere Ausführung nur bei
' ' Resultat OK:
' If .ShowDialog = Windows.Forms.DialogResult.OK Then
' txt = .FileName & vbNewLine
' ' Welcher Filter wurde verwendet?
' Dim idx As Integer = .FilterIndex
' txt &= "FilterIndex: " & idx.ToString & vbNewLine
' txt &= "Gewählter Filter: " & FilterDescription(.Filter, _
' idx)
' Else
' txt = "Abbruch durch Benutzer"
' End If
' End With
' End Using
' TextBox1.Text = txt
'End Sub 'SaveFileDialog
'###################################################################################################################
'############### xxxxxxxxxxxxxxxxxxxxxxx ###########################################################################
'###################################################################################################################
'Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As _
' Object, ByVal e As EventArgs) _
' Handles ComboBox1.SelectedIndexChanged
' Dim strFilter As String
' Select Case ComboBox1.SelectedIndex
' Case 0
' ' Einfacher Filter
' strFilter = "Gif Bilddateien|*.gif"
' Case 1
' ' Erweiterter Filter
' strFilter = "JPEG Bilddateien|*.jpg|Gif Bilddateien|*.gif|Alle Dateien|*.*"
' Case Else
' ' Mehrere Dateierweiterungen in einer Auswahl
' strFilter = "Alle Bilddateien|*.jpg; *.jpeg; *.bmp; *.gif, *.tif|Alle Dateien|*.*"
' End Select
' TextBox2.Text = strFilter
'End Sub 'ComboBox1_SelectedIndexChanged
'Private Function FilterDescription(ByVal Filter As String, _
' ByVal Index As Integer) As String
' ' erstellt aus dem Filterstring die Bezeichnung
' Dim strArr() As String = Split(Filter, "|"), _
' MyList As New List(Of String)
' For i As Integer = 0 To strArr.GetUpperBound(0)
' If (i Mod 2 = 0) Then MyList.Add(strArr(i))
' Next
' Return MyList(Index - 1)
'End Function 'FilterDescription
'###################################################################################################################
'############### 2.Config Load/Save ################################################################################
'############### Methonden für Laden von Variablen aus DB;INI,... ##################################################
'###################################################################################################################
' 2.1 From INI
' 2.1.1 Config_INI_ReadValue
' 2.1.2 Config_INI_WriteValue
' 2.2 From My CMD seting Files
'###################################################################################################################
'############### 2.1 Config_From_INI ###############################################################################
'###################################################################################################################
'FamExample = Config_INI_ReadValue(FilePath, Section, Key, [""])
'Config_INI_WriteValue(FilePath, Section, Key, Wert)
Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Ansi Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32
Public Function Config_INI_ReadValue(ByVal sFilePath As String, ByVal sSection As String, ByVal sKey As String, Optional ByVal sDefault As String = "") As String
Dim strTemp As String = Space(1024), lLength As Integer
lLength = GetPrivateProfileString(sSection, sKey, sDefault, strTemp, strTemp.Length, sFilePath)
Return (strTemp.Substring(0, lLength))
End Function
Public Function Config_INI_WriteValue(ByVal sFilePath As String, ByVal sSection As String, ByVal sKey As String, ByVal sValue As String) As Boolean
Return (Not (WritePrivateProfileString(sSection, sKey, sValue, sFilePath) = 0))
End Function
'###################################################################################################################
'############### 2.2 Config_From_My-CMD-File #######################################################################
'###################################################################################################################
'Syntax der Datei:
'Variable1 = Wertxy
'Variable2 = Zahtl2
'...
'Alles in String (Mussa auf zahl Überprüft werden und dan DeDim as Integer ...)
'###################################################################################################################
'############### 3.PortableMode ####################################################################################
'############### Veststellen ob Portable und deren Eigenarten ######################################################
'###################################################################################################################
' 3.1 3.PortableModeTrue
'Festlegung ob Portable oder nicht
Public Function PortableModeTrue(ByVal ConfigFileName As String) As Boolean
If File.Exists(".\" & ConfigFileName) Then
PortableModeTrue = True
Else
PortableModeTrue = False
End If
End Function 'PortableModeTrue
'Übergabe: ConfigFile [WorkDir] [AppName] Return: (PortableMode=True/False)
'ConfigFile Nahme und Extendion - Wird Indirekt Geändert zu KonfigPfad
'WorkDir Pfad des Arbeitsverzeichnises - Wird Indirekt Geändert
'AppName Erstellt einen Neuern Ordner mit Diesen Nahme, wenn Nicht Portabler Mode
Public Function PortableMode(ByRef ConfigFile As String, Optional ByRef WorkDir As String = "-default-", Optional ByVal AppName As String = "-default-") As Boolean
If AppName = ("-default-" Or "") Then
AppName = Application.ProductName
End If
If Not Directory.Exists(WorkDir) Then WorkDir = "-default-"
If WorkDir = ("-default-" Or "") Then
WorkDir = Application.StartupPath
End If
If File.Exists(Path.Combine(WorkDir, ConfigFile)) Then
PortableMode = True
ConfigFile = Path.Combine(WorkDir, ConfigFile)
Else
PortableMode = False
WorkDir = Environment.GetEnvironmentVariable("APPDATA") & "\" & AppName
If Not Directory.Exists(WorkDir) Then Directory.CreateDirectory(WorkDir)
ConfigFile = Path.Combine(WorkDir, ConfigFile)
If Not File.Exists(ConfigFile) Then File.Create(ConfigFile)
End If
End Function 'PortableMode
'###################################################################################################################
'############### 4.GPS_Tools #######################################################################################
'###################################################################################################################
Public Sub GPS_to_GMaps(ByVal GPS As String)
'Soll anhand der GPS Koordinaten GoogleMaps öfnen
'URL ansteuerung: https://developers.google.com/maps/documentation/staticmaps/
' String = "52.520817 13.40945"
'
'Umrechnung von Geo-Koordinaten: http://rechneronline.de/geo-koordinaten/
' ___________________________________________________________________________________________________
'| Grad, Minuten, Sekunden | Dezimalgrad | Grad, Dezimalminuten |
'| z.B. N52° 31' 14.941" E13° 24' 34.020" | z.B. 52.520817 13.40945 | z.B. N52° 31.249 E13° 24.567 |
' ---------------------------------------------------------------------------------------------------
' URL= "http://maps.google.com/maps?q=" & String
'In Projekt SB-PictureTagger: Set a LincLable ;)
End Sub
'###################################################################################################################
'############### 5.Ascii<->Base64 ##################################################################################
'###################################################################################################################
'Ascii to Base64
Public Function Ascii_To_Base64(ByVal AsciiString As String) As String
Dim base64encoded As String = vbNull
Dim raw As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(AsciiString)
base64encoded = System.Convert.ToBase64String(raw)
Ascii_To_Base64 = base64encoded
End Function
'Base64 to Ascii
Public Function Base64_To_Ascii(ByVal Base64String As String) As String
Try
Dim raw As Byte() = System.Convert.FromBase64String(Base64String)
Base64_To_Ascii = System.Text.ASCIIEncoding.ASCII.GetString(raw)
Catch ex As Exception
Base64_To_Ascii = ""
End Try
End Function
End Module 'Modul_Tools