Imports System.Net
Imports System.IO.Directory
Imports System.Net.WebRequestMethods
Imports System.Drawing.Printing
Imports System.Globalization
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Public Class Scanbutton
'Deklaration
Dim ziel As String
Dim user As String
Dim pwd As String
Dim datum As String
Dim ofd As New OpenFileDialog
Dim sfd As New SaveFileDialog
Dim fso As System.Object
Dim imagepfad As String
Dim dialog As New WIA.CommonDialog
Dim image As WIA.ImageFile = Nothing
Dim i As String
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const ConnectTimeOut = 30
Private Sub Scanbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Zugangsdaten
user = ("Benutzer")
pwd = ("Passwort")
i = 0
fso = New Object
'Datum Formatierung
datum = FormatDateTime(Now, DateFormat.ShortTime)
datum = Replace(datum, " ", "_") ' leerzeichen durch unterstrich ersetzen
datum = Replace(datum, ".", "") ' Punkte entfernen
datum = Replace(datum, ":", "") ' Doppelpunkte entfernen
MsgBox("Legen Sie ein Dokument in Ihren Scanner.")
'Hierbei wird die Windows GUI aufgerufen die zum Scannen verwendet werden kann.
Try
image = dialog.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, , , , False, )
Catch ex As Exception
MsgBox("Ein Fehler ist aufgetreten! Überprüfen Sie ob das Gerät Eingeschaltet und angeschlossen ist." & Environment.NewLine & Environment.NewLine & ex.Message)
End
End Try
If Not image Is Nothing Then
imagepfad = ("C:\Scanbilder\")
If FileExists(imagepfad + "img_" + datum + ".jpg") Then
i = i + 1
image.SaveFile(imagepfad + "img_" + datum + "_" + i + ".jpg")
Else
image.SaveFile(imagepfad + "img_" + datum + ".jpg")
End If
Else
MsgBox("Es wurde kein Bild erfasst!", MsgBoxStyle.Information, "Information")
End
End If
ofd.InitialDirectory = "C:\Scanbilder\"
ofd.Filter = "Gescannte Dokumente (*.jpg)|*.jpg"
ofd.Title = "Datei zum Öffnen auswählen"
If ofd.ShowDialog() = DialogResult.OK Then
MsgBox("Datei wurde ausgewählt.")
Else
MsgBox("Abbruch durch User.")
End
End If
Dim webclient As New Net.WebClient
webclient.Credentials = New Net.NetworkCredential(user, pwd)
ziel = ("FTPServer")
Dim request As Net.FtpWebRequest = Net.FtpWebRequest.Create(ziel)
request.Method = Net.WebRequestMethods.Ftp.ListDirectory
request.Credentials = New Net.NetworkCredential(user, pwd)
Try
request.GetResponse()
MsgBox("Connected")
Catch ex As Exception
MsgBox("Kein Connect")
Close()
End Try
My.Computer.Network.DownloadFile(ofd.FileName, ofd.SafeFileName, user, pwd)
If ftpfileExists(ziel) = True Then
ziel = ("FTPServer/" + ofd.SafeFileName)
My.Computer.Network.UploadFile(ofd.FileName, ziel, user, pwd, True, 500)
MsgBox("Erfolgreiches Hochladen!")
End
Else
MsgBox("Der Zielordner zum Einfügen der gescannten Dokumente, ist nicht verfügbar.")
MsgBox("Überprüfen Sie ob der Zielordner vorhanden ist.")
End
End If
End Sub
'########################################################################################################################################
' Funktion zum überprüfen eines Ordners
'########################################################################################################################################
Public Function DirExists(ByVal OrigFolder As String)
Dim fs
fs = CreateObject("Scripting.FileSystemObject")
DirExists = fs.folderexists(OrigFolder)
End Function
'########################################################################################################################################
' Funktion zum überprüfen einer Datei
'########################################################################################################################################
Private Function FileExists(ByVal FileName As String) As Boolean
On Error Resume Next
FileExists = Not CBool(GetAttr(FileName) And (vbDirectory Or vbVolume))
On Error GoTo 0
End Function
'########################################################################################################################################
' Funktion zum überprüfen einer Datei auf dem FTP-Server
'########################################################################################################################################
Private Function ftpfileExists(ByVal FileName As String)
If (My.Computer.Network.IsAvailable = True) Then
MsgBox("Hat geklappt")
Return (True)
End
Else
MsgBox("Hat nicht geklappt")
Return (False)
End
End If
End Function
'########################################################################################################################################
' Button Upload um Scanvorgang zu umgehen
'########################################################################################################################################
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles einfacher_upload.Click
MsgBox("Wählen Sie eine Datei aus, die Sie hochladen möchten.")
upload()
End Sub
'########################################################################################################################################
' Funktion Upload einer Datei für den Button Upload
'########################################################################################################################################
Private Function upload() As Action
ofd.InitialDirectory = "C:\Scanbilder\"
ofd.Filter = "Gescannte Dokumente (*.jpg)|*.jpg"
ofd.Title = "Datei zum Öffnen auswählen"
If ofd.ShowDialog() = DialogResult.OK Then
MsgBox("Datei wurde ausgewählt.")
Else
MsgBox("Abbruch durch User.")
End
End If
ziel = ("FTPServer/ftpsonstige/Scanbilder/")
If ftpfileExists(ziel) = True Then
ziel = ("FTPServer/ftpsonstige/Scanbilder/" + ofd.SafeFileName)
My.Computer.Network.UploadFile(ofd.FileName, ziel, user, pwd, True, 500)
MsgBox("Erfolgreiches Hochladen!")
End
Else
MsgBox("Der Zielordner zum Einfügen der gescannten Dokumente, ist nicht verfügbar.")
MsgBox("Überprüfen Sie ob der Zielordner vorhanden ist.")
End
End If
End Function