473,732 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ServiceP rocess
Imports System.Diagnost ics
Imports System.Net.Http WebRequest
Imports System.Net.Http WebResponse
Public Class SHIM
Inherits System.ServiceP rocess.ServiceB ase

Private tw As TextWriter
Private eLog As New EventLog("", ".", "SHIM")

'Private HttpWReq As HttpWebRequest =
CType(WebReques t.Create("https ://trading.harbors ideplus.com"),
HttpWebRequest)
'Private HttpWReq As HttpWebRequest =
WebRequest.Crea te("https://trading.harbors ideplus.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.
InitializeCompo nent()
' Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceP rocess.ServiceB ase

' 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.ServiceP rocess.ServiceB ase ()
{New Service1, New MySecondUserSer vice}
'
ServicesToRun = New System.ServiceP rocess.ServiceB ase() {New
SHIM}

System.ServiceP rocess.ServiceB ase.Run(Service sToRun)
End Sub

'Required by the Component Designer
Private components As System.Componen tModel.IContain er

' 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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
'
'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.T imer 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.KeepA live = False
'' Allow Redirection.
'HttpWReq.Maxim umAutomaticRedi rections = 4service
'HttpWReq.Allow AutoRedirect = True
'' Set the 'Timeout' property of the HttpWebRequest to 10
milli seconds.
'HttpWReq.Timeo ut = 3600
''Display the 'Timeout' property of the 'HttpWebRequest ' on
the console.
''Console.Write Line("\nThe timeout time of the request after
setting the timeout is {0} milliSeconds.", myHttpWebReques t.Timeout);

'' Get the HTTP protocol version number returned by the
server.
'Dim ver As String = HttpWResp.Proto colVersion.ToSt ring()
'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(sS tring, True)
sr.WriteLine("T his is a test...")
writeLine("Star t of Log...")
'If File.Exists(sSt ring) Then
' Console.WriteLi ne("{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(fo rmatText(sStrin g))
End Sub
Private Function formatText(ByVa l sString As String) As String
Dim ss As String

ss = String.Concat(d t.Now.ToString( "F"), ": ", sString)
Return ss
End Function
End Class
Nov 20 '05 #1
0 1665

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

Similar topics

0
1223
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 controllor in MMC, but actually, I accomplished the service without configuration and have no idea about what I can do next! In my project I write a service to do mutliple tasks by schedule, and the time schedule was saved on a xml file, as my thought,...
1
2193
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 base Classes in .NET make writing a Windows Service Easy" says it should be there. What could be wrong ??? I have the service installer components - added but that interface to add those components is different. Thanks
3
12503
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 to the registry key. What can cause to this message. Also I want to create sub key from my asp.net project. How can I authoritize to write a new for the web application for the registry. Thanks for your answers.
0
929
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 a class called TestClass (inherits Marshallbyrefobject for remoting). I then added the built assembly to the bin folder on the RemotingHost website. I created a web config file with system.runtime.remoting wellknown service for the testclass as...
2
1776
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 application for configuring and reporting. They are on a simple local network, just the 3 machines, no wider access. We do a certain amount of communication between them, using UDP and MessageQueueing and also in one instance a VB.Net Windows Forms...
3
7964
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 C:\Inetpub\wwwroot\RunReportASP is the directory of my virtual directory for my app. In it's properties I did check "Write" as a permission. What else do I need to do?
0
4286
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. variables are fine and NETWORK SERVICE has full access to ASP .NET temp folder. Please help, Thank you! IK
2
11326
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 files, can call custom code for each file received - much like BizTalk) 1. Receive multiple files, each file received creates a "WriteToFile" object
5
12580
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 existing app.pool gives same results). This webapplication tries to write to a log file. Used Process Monitor (from Sysinternals) for monitoring purposes. Logged in to my machine as 'MYPC\danny', a local account that's a member of the administrators...
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.