473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Cant write to file from windows service

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
Nov 20 '05 #1
0 1641

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: winking | last post by:
Hello. I write a windows service project with C sharp, yeah, a trouble contact me while i design a GUI configuration for my service, which i hope that it would be like microsoft windows IIS...
1
by: andrewcw | last post by:
I am building the basic windows service, but on my tool box tab , there are no components, just clipboard Ring,l pointer & general. But the MSDN article Dec 2001 by Getz, "windows services: New...
3
by: caldera | last post by:
Hi, I write a windows service. In that service I create a new subkey for registry. But service install is call CreateSubKey() the program throw an exception to the event log said that Cannot write...
0
by: Mark Shehan | last post by:
I am using VS2005 and ASP.NET 2 I want to use IIS to host some .NET remoting. I have a website I created called RemotingHost. I created another class project called RemotingServer and in there...
2
by: felecha | last post by:
I'm working on a system that has a several computers that run an alerting system. There are 2 "Host" machines that have no user interface, and one "Admin" station that has an Administrator's...
3
by: David Thielen | last post by:
Hi; I created a virtual directory in IIS 6.0 and my asp.net app runs fine. But when it tries to write a file I get: Access to the path is denied. - C:\Inetpub\wwwroot\RunReportASP\images ...
0
by: IKdev | last post by:
Hi all, I have win2003 server and NET 1.1 framework. Created simple ASP .NET app with one page that has button and a label. When trying to run got the CS0016 error (see below). My temp env....
2
by: =?Utf-8?B?RGFtZW9u?= | last post by:
Hi - I am attempting to write lines to a file at high volume, multiple threads. Here is my scenario: (initial "WriteToFile" object created via a parent multithreaded process, which receives...
5
by: DotNetDanny | last post by:
Hello Machine: Windows Vista Business, standalone machine (no domain). Installed an old classic ASP webapplication in IIS7, running under a new app.pool with 'NETWORK SERVICE' account (using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.