I have found this code from thread796-317791.
I want a similar application that checks a folder for a file every few minutes then emails the files as an attachment.
I am new to .net and want to modify this code to achieve the above.
I have pasted the code but get the following error "Namespace or type ServiceProcess for the Imports System ServiceProcess cannot be found", could somebody let me know what I need to do to correct it?
Thanks in advance.
Here is the code:
Imports System.ServiceProcess
Imports System.IO
Imports System.Net
Public Class win321xpCompat
Inherits System.ServiceProcess.ServiceBase
Dim theIP As String
Dim theInterval As Long
Dim worker As request
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New win321xpCompat()}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "win321xpCompat"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
'get configuration settings from local cfg file
' if getConfig fails, then just abort --
' service will still say "started", but will do nothing
Try
Call getConfig(theIP, theInterval)
Catch
Return
End Try
'ding it one time on service start
worker = New request(theIP, theInterval)
worker.startWork()
'set up a timer to raise an event on theInterval
' at which time it will be dinged again
Dim aTimer As New System.Timers.Timer()
AddHandler aTimer.Elapsed, AddressOf onNewRequest
aTimer.Interval = theInterval * 60000
aTimer.Enabled = True
Return
End Sub
'file 'win32xp.cfg' must reside on c:\ or this routine fails
' the exception is handled above and simply aborts the timer
' so that the service becomes useless...
Private Sub getConfig(ByRef ip As String, ByRef interval As Long)
Dim streamreaderobj As StreamReader
Dim filecont As String
Dim myArray() As String
streamreaderobj = File.OpenText("c:\win32xp.cfg")
filecont = streamreaderobj.ReadLine()
Do Until filecont = ""
myArray = filecont.Split("=")
If myArray(0) = "address" Then
ip = myArray(1)
Else
interval = CType(myArray(1), Long)
End If
filecont = streamreaderobj.ReadLine()
Loop
streamreaderobj.Close()
End Sub
'this is the routine that's called by the timer.
' it makes a single request and quits
Private Sub onNewRequest(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
worker = New request(theIP, theInterval)
worker.startWork()
End Sub
'the following class makes a request to an address.
' it doesn't care what the response is, even if it's
' a 404 or some other error... it just does it's thing
' and quits.
Private Class request
Dim mvarip As String
Dim mvarinterval As Long
Dim continue As Boolean
Public Sub New(ByVal ip As String, ByVal interval As Long)
mvarip = ip
mvarinterval = interval * 60000
continue = True
End Sub
Public Sub startWork()
Dim myRequest As httpWebRequest = CType(WebRequest.Create(" & mvarip), httpWebRequest)
Dim response As WebResponse
response = myRequest.GetResponse()
myRequest = Nothing
response = Nothing
End Sub
End Class
End Class
I want a similar application that checks a folder for a file every few minutes then emails the files as an attachment.
I am new to .net and want to modify this code to achieve the above.
I have pasted the code but get the following error "Namespace or type ServiceProcess for the Imports System ServiceProcess cannot be found", could somebody let me know what I need to do to correct it?
Thanks in advance.
Here is the code:
Imports System.ServiceProcess
Imports System.IO
Imports System.Net
Public Class win321xpCompat
Inherits System.ServiceProcess.ServiceBase
Dim theIP As String
Dim theInterval As Long
Dim worker As request
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New win321xpCompat()}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "win321xpCompat"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
'get configuration settings from local cfg file
' if getConfig fails, then just abort --
' service will still say "started", but will do nothing
Try
Call getConfig(theIP, theInterval)
Catch
Return
End Try
'ding it one time on service start
worker = New request(theIP, theInterval)
worker.startWork()
'set up a timer to raise an event on theInterval
' at which time it will be dinged again
Dim aTimer As New System.Timers.Timer()
AddHandler aTimer.Elapsed, AddressOf onNewRequest
aTimer.Interval = theInterval * 60000
aTimer.Enabled = True
Return
End Sub
'file 'win32xp.cfg' must reside on c:\ or this routine fails
' the exception is handled above and simply aborts the timer
' so that the service becomes useless...
Private Sub getConfig(ByRef ip As String, ByRef interval As Long)
Dim streamreaderobj As StreamReader
Dim filecont As String
Dim myArray() As String
streamreaderobj = File.OpenText("c:\win32xp.cfg")
filecont = streamreaderobj.ReadLine()
Do Until filecont = ""
myArray = filecont.Split("=")
If myArray(0) = "address" Then
ip = myArray(1)
Else
interval = CType(myArray(1), Long)
End If
filecont = streamreaderobj.ReadLine()
Loop
streamreaderobj.Close()
End Sub
'this is the routine that's called by the timer.
' it makes a single request and quits
Private Sub onNewRequest(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
worker = New request(theIP, theInterval)
worker.startWork()
End Sub
'the following class makes a request to an address.
' it doesn't care what the response is, even if it's
' a 404 or some other error... it just does it's thing
' and quits.
Private Class request
Dim mvarip As String
Dim mvarinterval As Long
Dim continue As Boolean
Public Sub New(ByVal ip As String, ByVal interval As Long)
mvarip = ip
mvarinterval = interval * 60000
continue = True
End Sub
Public Sub startWork()
Dim myRequest As httpWebRequest = CType(WebRequest.Create(" & mvarip), httpWebRequest)
Dim response As WebResponse
response = myRequest.GetResponse()
myRequest = Nothing
response = Nothing
End Sub
End Class
End Class