Files
AVISO/Aviso/frmWechselStandarddrucker.vb

114 lines
4.2 KiB
VB.net

Imports System.IO
Imports System.Runtime.InteropServices
Public Class frmWechselStandarddrucker
Private filePathListOfPrinters As List(Of String) = New List(Of String)
Private Sub frmWechselStandarddrucker_Load(sender As Object, e As EventArgs) Handles MyBase.Load
filePathListOfPrinters.Add(FileIO.SpecialDirectories.MyDocuments & "\AVISO_OPTIONS.txt")
filePathListOfPrinters.Add("C:\AVISO_OPTIONS.txt")
For Each path As String In filePathListOfPrinters
getCurrentPrintersFromFile(path)
Next
SDL.cProgramFunctions.initDrucker(cboDefaultprinter, VERAG_PROG_ALLGEMEIN.cAllgemein.PRINTER_STANDARD, False)
SDL.cProgramFunctions.initDrucker(cboDefaultprinterBON, VERAG_PROG_ALLGEMEIN.cAllgemein.PRINTER_BON, False)
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
If cboDefaultprinterBON.SelectedItem Is Nothing Or cboDefaultprinter.SelectedItem Is Nothing Then Me.Close()
VERAG_PROG_ALLGEMEIN.cAllgemein.PRINTER_STANDARD = cboDefaultprinter._value
VERAG_PROG_ALLGEMEIN.cAllgemein.PRINTER_BON = cboDefaultprinterBON._value
Try
SetDefaultPrinter(cboDefaultprinter._value)
Catch ex As Exception
MsgBox(ex.Message)
End Try
For Each path As String In filePathListOfPrinters
changetxtFileForPrinter(path)
Next
MsgBox("Drucker wurden geändert")
End Sub
Private Function changetxtFileForPrinter(printerFile As String) As Boolean
Dim changed As Boolean = False
Try
If File.Exists(printerFile) Then
Dim l = IO.File.ReadAllLines(printerFile, System.Text.Encoding.Default)
If l.Count > 0 Then
If Not String.IsNullOrWhiteSpace(cboDefaultprinter._value) Then
Dim lines As List(Of String) = IO.File.ReadAllLines(printerFile).ToList
For index As Integer = 0 To lines.Count - 1
If lines(index).ToLower.StartsWith("printer") Then
lines(index) = String.Concat("PRINTER:", cboDefaultprinter._value)
changed = True
End If
Next
If changed Then IO.File.WriteAllLines(printerFile, lines.ToArray)
End If
If Not String.IsNullOrWhiteSpace(cboDefaultprinterBON._value) Then
Dim lines As List(Of String) = IO.File.ReadAllLines(printerFile).ToList
For index As Integer = 0 To lines.Count - 1
If lines(index).ToLower.StartsWith("bon") Then
lines(index) = String.Concat("BON:", cboDefaultprinterBON._value)
changed = True
End If
Next
If changed Then IO.File.WriteAllLines(printerFile, lines.ToArray)
End If
End If
End If
Return changed
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Private Sub getCurrentPrintersFromFile(printerFile As String)
Try
If File.Exists(printerFile) Then
Dim l = IO.File.ReadAllLines(printerFile, System.Text.Encoding.Default)
If l.Count > 0 Then
For Each li In l
If li.StartsWith("BON:") Then
VERAG_PROG_ALLGEMEIN.cAllgemein.PRINTER_BON = (li.Replace("BON:", "")).Trim
End If
If li.StartsWith("PRINTER:") Then
VERAG_PROG_ALLGEMEIN.cAllgemein.PRINTER_STANDARD = (li.Replace("PRINTER:", "")).Trim
End If
Next
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
<DllImport("winspool.drv", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Shared Function SetDefaultPrinter(Name As String) As Boolean
End Function
End Class