For some reason my service works fine except that it will create the
file in my c drive, but will not write to the file.
Sorry if this is a duplicate post, i have found some that ask the same
question - but noone seems to answer.
Do i need to do anything specific to write to a text file from a
windows service, much appreciated.
Imports System
Imports System.ServiceProcess
Imports System.Diagnostics
Imports System.Net.HttpWebRequest
Imports System.Net.HttpWebResponse
Public Class SHIM
Inherits System.ServiceProcess.ServiceBase
Private tw As TextWriter
Private eLog As New EventLog("", ".", "SHIM")
'Private HttpWReq As HttpWebRequest =
CType(WebRequest.Create("https://trading.harborsideplus.com"),
HttpWebRequest)
'Private HttpWReq As HttpWebRequest =
WebRequest.Create("https://trading.harborsideplus.com")
'Private HttpWResp As HttpWebResponse =
CType(HttpWReq.GetResponse(), HttpWebResponse)
#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
SHIM}
System.ServiceProcess.ServiceBase.Run(ServicesToRu n)
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()
'
'SHIM
'
Me.ServiceName = "SHIM"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set
things
' in motion so your service can do its work.
tw = New TextWriter("c:\shim.log")
eLog.WriteEntry("In SHIM onStart...")
'Note A service application is designed to be long running.
'As such, it usually polls or monitors something in the
system.
'The monitoring is set up in the OnStart method. However,
'OnStart does not actually do the monitoring.
'The OnStart method must return to the operating system once
the service's operation has begun.
'It must not loop forever or block. To set up a simple polling
mechanism,
'you can use the System.Timers.Timer component.
'In the OnStart method, you would set parameters on the
component,
'and then you would set the Timer.Enabled property to true.
'The timer would then raise events in your code periodically,
'at which time your service could do its monitoring.
'' Turn off connection keep-alives.
'HttpWReq.KeepAlive = False
'' Allow Redirection.
'HttpWReq.MaximumAutomaticRedirections = 4service
'HttpWReq.AllowAutoRedirect = True
'' Set the 'Timeout' property of the HttpWebRequest to 10
milli seconds.
'HttpWReq.Timeout = 3600
''Display the 'Timeout' property of the 'HttpWebRequest' on
the console.
''Console.WriteLine("\nThe timeout time of the request after
setting the timeout is {0} milliSeconds.",myHttpWebRequest.Timeout);
'' Get the HTTP protocol version number returned by the
server.
'Dim ver As String = HttpWResp.ProtocolVersion.ToString()
'tw.writeLine("The Harborside Plus Web Server Version is: " +
ver)
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop
your service.
eLog.WriteEntry("In SHIM onStop...")
End Sub
End Class
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class TextWriter
Private sr As StreamWriter
Private dt As DateTime
Public Sub New(ByVal sString As String)
dt = New DateTime
sr = New StreamWriter(sString, True)
sr.WriteLine("This is a test...")
writeLine("Start of Log...")
'If File.Exists(sString) Then
' Console.WriteLine("{0} already exists.", FILE_NAME)
' Return
'End If
End Sub
Protected Overrides Sub Finalize()
sr.Close()
MyBase.Finalize()
End Sub
Public Sub writeLine(ByVal sString As String)
sr.WriteLine(formatText(sString))
End Sub
Private Function formatText(ByVal sString As String) As String
Dim ss As String
ss = String.Concat(dt.Now.ToString("F"), ": ", sString)
Return ss
End Function
End Class