Imports System.Runtime.InteropServices
Imports WinSCP
Imports System.Threading
Public Class MyNewService
Public eventID As Integer
Declare Auto Function SetServiceStatus Lib "advapi32.dll" (ByVal handle As IntPtr, ByRef serviceStatus As ServiceStatus) As Boolean
Public Sub New()
MyBase.New()
' This call is required by the designer.
InitializeComponent()
Try
' Add any initialization after the InitializeComponent() call.
Me.EventLog1 = New System.Diagnostics.EventLog
If Not System.Diagnostics.EventLog.SourceExists("MyNewService") Then
System.Diagnostics.EventLog.CreateEventSource("MyNewService", "MyNewLog")
End If
EventLog1.Source = "MyNewService"
EventLog1.Log = "MyNewLog"
Catch err As System.Diagnostics.Eventing.Reader.EventLogException
MsgBox(err.InnerException)
End Try
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
Dim serviceStatus As ServiceStatus = New ServiceStatus()
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING
serviceStatus.dwWaitHint = 100000
SetServiceStatus(Me.ServiceHandle, serviceStatus)
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING
SetServiceStatus(Me.ServiceHandle, serviceStatus)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
EventLog1.WriteEntry("Service Started")
Dim timer As System.Timers.Timer = New System.Timers.Timer()
timer.Enabled = True
timer.Interval = 60000
AddHandler timer.Elapsed, AddressOf Me.OnTimer
timer.Start()
End Sub
Private Sub OnTimer(sender As Object, e As Timers.ElapsedEventArgs)
' TODO: Insert Monitoring Activies Here.
Try
EventLog1.WriteEntry("Checking For New Files", EventLogEntryType.Information, eventID)
' Setup session options
Dim sessionOptions As New SessionOptions
EventLog1.WriteEntry("SessionOptions", EventLogEntryType.Information, eventID)
With sessionOptions
.Protocol = Protocol.Sftp
.HostName = "XXXXXXXXXXX"
.UserName = "XXXXXXXXXXX"
.Password = "XXXXXXXXXXX"
.SshHostKeyFingerprint = "XXXXXXXXXXXXXXXX"
End With
Using session As New Session
' Connect
session.Open(sessionOptions)
EventLog1.WriteEntry("Session To Cheyney FTP Open")
' Download files
Dim transferOptions As New TransferOptions
transferOptions.TransferMode = TransferMode.Binary
Dim transferResult As TransferOperationResult
transferResult = session.GetFiles("/Inbox1/*", "C:\Localfolder\", False, transferOptions)
' Throw on any error
transferResult.Check()
' Print results
For Each transfer In transferResult.Transfers
EventLog1.WriteEntry(".....Transferring File:" & transfer.filename)
Next
End Using
Catch err As Exception
EventLog1.WriteEntry("Service Exception: " & err.InnerException.ToString)
End Try
eventID = eventID + 1
End Sub
Protected Overrides Sub OnStop()
Dim serviceStatus As ServiceStatus = New ServiceStatus()
serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED
SetServiceStatus(Me.ServiceHandle, serviceStatus)
' Add code here to perform any tear-down necessary to stop your service.
EventLog1.WriteEntry("Service Stopped")
End Sub
Public Enum ServiceState
SERVICE_STOPPED = 1
SERVICE_START_PENDING = 2
SERVICE_STOP_PENDING = 3
SERVICE_RUNNING = 4
SERVICE_CONTINUE_PENDING = 5
SERVICE_PAUSE_PENDING = 6
SERVICE_PAUSED = 7
End Enum
<StructLayout(LayoutKind.Sequential)>
Public Structure ServiceStatus
Public dwServiceType As Long
Public dwCurrentState As ServiceState
Public dwControlsAccepted As Long
Public dwWin32ExitCode As Long
Public dwServiceSpecificExitCode As Long
Public dwCheckPoint As Long
Public dwWaitHint As Long
End Structure
End Class