473,563 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default path for Windows Service

I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.Exe cutablePath.Sub string(0,
Application.Exe cutablePath.Las tIndexOf("\")) ' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would prefer
the former because I do not know the layout of the target computer.

--
Anil Gupte
www.keeninc.net
www.icinema.com
Sep 13 '07 #1
12 3694
On Sep 13, 11:39 am, "Anil Gupte" <anil-l...@icinema.co mwrote:
I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.Exe cutablePath.Sub string(0,
Application.Exe cutablePath.Las tIndexOf("\")) ' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would prefer
the former because I do not know the layout of the target computer.

--
Anil Guptewww.keenin c.netwww.icinem a.com
Try the WMI class to get Windows Default path

Imports System.Manageme nt
Public Class wmi
Private objOS As ManagementObjec tSearcher
Private objCS As ManagementObjec tSearcher
Private objMgmt As ManagementObjec t
Private m_strComputerNa me As String
Private m_strManufactur er As String
Private m_StrModel As String
Private m_strOSName As String
Private m_strOSVersion As String
Private m_strSystemType As String
Private m_strTPM As String
Private m_strWindowsDir As String
Public Sub New()

objOS = New ManagementObjec tSearcher("SELE CT * FROM
Win32_Operating System")
objCS = New ManagementObjec tSearcher("SELE CT * FROM
Win32_ComputerS ystem")
For Each objMgmt In objOS.Get
m_strOSName = objMgmt("name") .ToString()
m_strOSVersion = objMgmt("versio n").ToString ()
m_strComputerNa me = objMgmt("csname ").ToString ()
m_strWindowsDir = objMgmt("window sdirectory").To String()
Next

For Each objMgmt In objCS.Get
m_strManufactur er = objMgmt("manufa cturer").ToStri ng()
m_StrModel = objMgmt("model" ).ToString()
m_strSystemType = objMgmt("system type").ToString
m_strTPM = objMgmt("totalp hysicalmemory") .ToString()
Next
End Sub

Public ReadOnly Property ComputerName()
Get
ComputerName = m_strComputerNa me
End Get

End Property
Public ReadOnly Property Manufacturer()
Get
Manufacturer = m_strManufactur er
End Get

End Property
Public ReadOnly Property Model()
Get
Model = m_StrModel
End Get

End Property
Public ReadOnly Property OsName()
Get
OsName = m_strOSName
End Get

End Property

Public ReadOnly Property OSVersion()
Get
OSVersion = m_strOSVersion
End Get

End Property
Public ReadOnly Property SystemType()
Get
SystemType = m_strSystemType
End Get

End Property
Public ReadOnly Property TotalPhysicalMe mory()
Get
TotalPhysicalMe mory = m_strTPM
End Get

End Property

Public ReadOnly Property WindowsDirector y()
Get
WindowsDirector y = m_strWindowsDir
End Get

End Property
Public ReadOnly Property FontDirectory()
Get
FontDirectory = m_strWindowsDir & "\font".ToU pper
End Get
End Property
End Class

Sep 13 '07 #2
Well that might be all very well if the OP wanted the path to the Windows
directory.

But he doesn't. He wants the path of the directory where the
application/service executable resides.
"Omar Abid" <om***********@ gmail.comwrote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
On Sep 13, 11:39 am, "Anil Gupte" <anil-l...@icinema.co mwrote:
>I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.Exe cutablePath.Sub string(0,
Application.Ex ecutablePath.La stIndexOf("\")) ' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would
prefer
the former because I do not know the layout of the target computer.

--
Anil Guptewww.keenin c.netwww.icinem a.com

Try the WMI class to get Windows Default path

Imports System.Manageme nt
Public Class wmi
Private objOS As ManagementObjec tSearcher
Private objCS As ManagementObjec tSearcher
Private objMgmt As ManagementObjec t
Private m_strComputerNa me As String
Private m_strManufactur er As String
Private m_StrModel As String
Private m_strOSName As String
Private m_strOSVersion As String
Private m_strSystemType As String
Private m_strTPM As String
Private m_strWindowsDir As String
Public Sub New()

objOS = New ManagementObjec tSearcher("SELE CT * FROM
Win32_Operating System")
objCS = New ManagementObjec tSearcher("SELE CT * FROM
Win32_ComputerS ystem")
For Each objMgmt In objOS.Get
m_strOSName = objMgmt("name") .ToString()
m_strOSVersion = objMgmt("versio n").ToString ()
m_strComputerNa me = objMgmt("csname ").ToString ()
m_strWindowsDir = objMgmt("window sdirectory").To String()
Next

For Each objMgmt In objCS.Get
m_strManufactur er = objMgmt("manufa cturer").ToStri ng()
m_StrModel = objMgmt("model" ).ToString()
m_strSystemType = objMgmt("system type").ToString
m_strTPM = objMgmt("totalp hysicalmemory") .ToString()
Next
End Sub

Public ReadOnly Property ComputerName()
Get
ComputerName = m_strComputerNa me
End Get

End Property
Public ReadOnly Property Manufacturer()
Get
Manufacturer = m_strManufactur er
End Get

End Property
Public ReadOnly Property Model()
Get
Model = m_StrModel
End Get

End Property
Public ReadOnly Property OsName()
Get
OsName = m_strOSName
End Get

End Property

Public ReadOnly Property OSVersion()
Get
OSVersion = m_strOSVersion
End Get

End Property
Public ReadOnly Property SystemType()
Get
SystemType = m_strSystemType
End Get

End Property
Public ReadOnly Property TotalPhysicalMe mory()
Get
TotalPhysicalMe mory = m_strTPM
End Get

End Property

Public ReadOnly Property WindowsDirector y()
Get
WindowsDirector y = m_strWindowsDir
End Get

End Property
Public ReadOnly Property FontDirectory()
Get
FontDirectory = m_strWindowsDir & "\font".ToU pper
End Get
End Property
End Class
Sep 13 '07 #3
Thanx, I will try that.

--
Anil Gupte
www.keeninc.net
www.icinema.com

"Stephany Young" <noone@localhos twrote in message
news:el******** *****@TK2MSFTNG P06.phx.gbl...
An easier way than using the SubString and LastIndexOf methods is:

AppDir = System.IO.Path. GetDirectoryNam e(Application.E xecutablePath)

If you include a reference to System.Windows. Forms in your service then
you can still use that. You can include that reference without having to
include a form.

An alternative is:

System.Reflecti on.Assembly.Get ExecutingAssemb ly().Location

which, with some Imports ... can be written as:

AppDir =
Path.GetDirecto ryName([Assembly].GetExecutingAs sembly().Locati on)
"Anil Gupte" <an*******@icin ema.comwrote in message
news:eH******** ******@TK2MSFTN GP03.phx.gbl...
>>I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.Exe cutablePath.Sub string(0,
Application.Ex ecutablePath.La stIndexOf("\")) ' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would
prefer the former because I do not know the layout of the target
computer.

--
Anil Gupte
www.keeninc.net
www.icinema.com

Sep 13 '07 #4
An easier way than using the SubString and LastIndexOf methods is:

AppDir = System.IO.Path. GetDirectoryNam e(Application.E xecutablePath)

If you include a reference to System.Windows. Forms in your service then you
can still use that. You can include that reference without having to include
a form.

An alternative is:

System.Reflecti on.Assembly.Get ExecutingAssemb ly().Location

which, with some Imports ... can be written as:

AppDir = Path.GetDirecto ryName([Assembly].GetExecutingAs sembly().Locati on)
"Anil Gupte" <an*******@icin ema.comwrote in message
news:eH******** ******@TK2MSFTN GP03.phx.gbl...
>I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug. In that I used

AppDir = Application.Exe cutablePath.Sub string(0,
Application.Exe cutablePath.Las tIndexOf("\")) ' Where this application
Excutable sits

Now, when I am converting it to a Service, what path can I use? Or can I
specify a path in the service to always use as a data path? I would
prefer the former because I do not know the layout of the target computer.

--
Anil Gupte
www.keeninc.net
www.icinema.com
Sep 13 '07 #5
Anil Gupte wrote:
I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug.
Better still, put your "processing " into a class. Then you can
instantiate and run it from either a Service or, say, a Console
application. Even /easier/ to debug. :-)
In that I used
AppDir = Application.Exe cutablePath.Sub string(0,
Application.Exe cutablePath.Las tIndexOf("\"))
Have you come across System.IO.Path yet?
Now, when I am converting it to a Service, what path can I use?
I use this:

Dim sExeFile as String _
= System.Reflecti on.Assembly.Get EntryAssembly() .Location
Dim sExeDir as String _
= System.IO.Path. GetDirectory( sExeFile )
I would prefer the former because I do not know the layout
of the target computer.
HTH,
Phill W.
Sep 13 '07 #6
Better still, put your "processing " into a class. Then you can
instantiate and run it from either a Service or, say, a Console
application. Even /easier/ to debug. :-)
I am having a problem getting the service to start and I cannot debug it. I
installed it on a different computer using installutil.exe In the Services
list, it appears, and I can hit start. However, I get a message saying
something to the effect that ther service started and then stopped and that
this was typical of services that "have nothing to do". Mine should do
something! My code looks like this:

Imports System.ServiceP rocess
Imports System.Data.Ole Db
Imports WMEncoderLib

#Region " Component Designer generated code "

Public Class MediaEncoderSer vice
Inherits System.ServiceP rocess.ServiceB ase
Dim WithEvents MediaEncoder As WMEncoder
Protected Overrides Sub OnStart(ByVal args() As String)
MediaEncode()
End Sub
Private Sub MediaEncode()
etc...
End Sub
End Class

So, I can't debug it because it won't even start. The install gives no
errors. I am stumped.....
P.S. The onstop event is empty...

Thanx,
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:fc******** **@south.jnrs.j a.net...
Anil Gupte wrote:
>I wrote my Windows Service first as a regular Windows Exe because it is
easier to debug.

Better still, put your "processing " into a class. Then you can
instantiate and run it from either a Service or, say, a Console
application. Even /easier/ to debug. :-)
>In that I used
>AppDir = Application.Exe cutablePath.Sub string(0,
Application.Ex ecutablePath.La stIndexOf("\"))

Have you come across System.IO.Path yet?
>Now, when I am converting it to a Service, what path can I use?

I use this:

Dim sExeFile as String _
= System.Reflecti on.Assembly.Get EntryAssembly() .Location
Dim sExeDir as String _
= System.IO.Path. GetDirectory( sExeFile )
>I would prefer the former because I do not know the layout
of the target computer.

HTH,
Phill W.

Sep 13 '07 #7
Anil Gupte wrote:
I am having a problem getting the service to start and I cannot debug it. I
installed it on a different computer using installutil.exe In the Services
list, it appears, and I can hit start. However, I get a message saying
something to the effect that ther service started and then stopped and that
this was typical of services that "have nothing to do".
Some thoughts about Services:

When they are started, the Service Control infrastructure calls your
OnStart method. This must return within a fixed time interval, or the
Service gets reported as "failing to start".
Don't call your "main processing" directly from OnStart.

You [normally] have to provide a loop construct to keep the service
running. This doesn't happen automatically as it does in a Windows
Forms app.

If you have large "units of work", watch out for OnStop.
The Service Control infrastructure calls your OnStop routine, which
allows you to "clean up" what the service is doing. When you return
from OnStop, the infrastructure tears down your service process, no
matter what it might still be doing.

Here's how I do it:

Sub OnStart()
tmrStarter.Star t()
End Sub

Private Sub tmrStarter_Elap sed( ...
' Kill the Timer - we don't need it again
tmrStarter.Stop ()

m_bShutdownComp lete = False
Do While Not m_bShutdownRequ ested
DoProcessing()
System.Threadin g.Thread.Sleep( a_while )
Loop
m_bShutdownComp lete = True
End Sub

Sub OnStop()
m_bShutdownRequ ested = True
Do While Not m_bShutdownComp lete
System.Threadin g.Thread.Sleep( 1000 )
Loop
' Return from OnStop and your process gets torn down!
End Sub

Private m_bShutdownRequ ested as Boolean
Private m_bShutdownComp lete as Boolean

Private Sub DoProcessing()
' Do the useful work here

' If you have any loop constructs, you can include tests of
' m_bShutdownRequ ested, so that your service can respond
' quickly to stop requests.
End Sub

HTH,
Phill W.
Sep 13 '07 #8
Hmm, OK, a couple of issues.

1. My service will not even start. I tried putting a msgbox in the OnStart
event to see if would do anything at all, but even that did not show. That
means it is not even reaching the OnStart event. I am sure it will run my
main process fine if it gets there or at least I can start debugging -
question is how to make it get there.

2. I do have a timer in the code, but I am not sure I want to kill it. I
want the timer to start the main process every 10 minutes (it checks to see
if a file has been uploaded and if found, runs the encoder on it). Another
thing, do I have to start the timer in the OnStart even of the service?

Thanx,
--
Anil Gupte
www.keeninc.net
www.icinema.com

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:fc******** **@south.jnrs.j a.net...
Anil Gupte wrote:
>I am having a problem getting the service to start and I cannot debug it.
I installed it on a different computer using installutil.exe In the
Services list, it appears, and I can hit start. However, I get a message
saying something to the effect that ther service started and then stopped
and that this was typical of services that "have nothing to do".

Some thoughts about Services:

When they are started, the Service Control infrastructure calls your
OnStart method. This must return within a fixed time interval, or the
Service gets reported as "failing to start".
Don't call your "main processing" directly from OnStart.

You [normally] have to provide a loop construct to keep the service
running. This doesn't happen automatically as it does in a Windows Forms
app.

If you have large "units of work", watch out for OnStop.
The Service Control infrastructure calls your OnStop routine, which allows
you to "clean up" what the service is doing. When you return from OnStop,
the infrastructure tears down your service process, no matter what it
might still be doing.

Here's how I do it:

Sub OnStart()
tmrStarter.Star t()
End Sub

Private Sub tmrStarter_Elap sed( ...
' Kill the Timer - we don't need it again
tmrStarter.Stop ()

m_bShutdownComp lete = False
Do While Not m_bShutdownRequ ested
DoProcessing()
System.Threadin g.Thread.Sleep( a_while )
Loop
m_bShutdownComp lete = True
End Sub

Sub OnStop()
m_bShutdownRequ ested = True
Do While Not m_bShutdownComp lete
System.Threadin g.Thread.Sleep( 1000 )
Loop
' Return from OnStop and your process gets torn down!
End Sub

Private m_bShutdownRequ ested as Boolean
Private m_bShutdownComp lete as Boolean

Private Sub DoProcessing()
' Do the useful work here

' If you have any loop constructs, you can include tests of
' m_bShutdownRequ ested, so that your service can respond
' quickly to stop requests.
End Sub

HTH,
Phill W.

Sep 13 '07 #9
Anil Gupte wrote:
1. My service will not even start. I tried putting a msgbox in the OnStart
event to see if would do anything at all, but even that did not show. That
means it is not even reaching the OnStart event.
Not necessarily.
If a Windows Service displays a MsgBox, it gets shown on a "virtual
desktop" that is reserved for system processes. No user can get to see
this "desktop", so the service process would seem to hang (this is why
ASP doesn't have a MsgBox function).
At least it used to - Windows may be "clever" enough now to catch the
MsgBox and write it into the Event log instead.
I am sure it will run my main process fine if it gets there or at least
I can start debugging - question is how to make it get there.
Put a Sleep(15000) into OnStart. That will give you enough time to
attach the debugger to the running [service] process when you start it.
2. I do have a timer in the code, but I am not sure I want to kill it.
I want the timer to start the main process every 10 minutes.
And if your "real" processing takes longer than 10 minutes, what then?
Do you want two instances of the routine running at the same time?
it will.

Personally, I /only/ use the Timer to "sidestep" out of OnStart and to
launch a continuously running process with Sleep call in it for the
"gaps" between bouts of doing stuff. I've had problems where a missing
dependent assembly can cause the Timer-invoked routine to fail to load
but, since this method is invoked from the innards of the Framework,
there's no easy way to catch the resulting Exception.
With a coded loop, a simple Try .. Catch does the job.
Another thing, do I have to start the timer in the OnStart even of
the service?
Yes. Start and Stop timers explicitly; that way you can clearly see
what's going on.

HTH,
Phill W.
Sep 17 '07 #10

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

Similar topics

5
6739
by: | last post by:
Hi, I have a Windows Service that uses a referenced dotnet-dll. In my dll I set some public string to a stringvalue that I get from reading a xml-file(my config file). It works fine if I use it in a vb-form. But since Windows Services ? executes? in windows\system32 I can´t locate my xml-config- file. I don´t want to set different...
1
3252
by: bob | last post by:
I have created a simple Windows service in VB.Net which installs fine using InstallUtil.exe to install it to, for example "c:\test", or "c:\Windows\YellowBanana", but if I install it to "c:\Program Files\Test" it installs ok but will not start (no useful error message is given other than the usual annoying suggestion about having sufficient...
6
3415
by: Laszlo Zsolt Nagy | last post by:
Sorry, I realized that the import zlib was not executed from my (working) service. So here is the question: why can't I use zlib from a win32 service? Is there any way to make it working? >------------- >Python could not import the service's module > File "T:\Python\Projects\NamedConnector\Service.py", line 17, in ? > from Processor...
14
9361
by: Gianfranco | last post by:
Hi everybody, First of all, sorry for my english if it isn't perfect ;-p I have an error in an asp.net application. I have win2003 server, with iis 6. I'm developping with visual studio 2005, vb.net, framework 2 I'm trying to access some binary files stored in C: in server2003. if i
4
16970
by: Jim | last post by:
I am writing a Windows Service in VB.Net, and right upfront I need to pass the path to my config.xml file to the constructor of the class which reads it. In the old days, App.Path & "\config.xml" would have done nicely. I have tried using Reflection.Assembly.GetExecutingAssembly.Location but this is horrible! I can't use the easy...
1
4343
by: amit.vasu | last post by:
Hi I have created a web serivces using .net framework 2.0. When I try to execute the web service I get the following error. Failed to start monitoring changes to 'e:\Default Web Site' because access is denied. I am running windows 2003 sp1. Network Service account do have full permission on application folder and sub folder.
2
4552
by: Ronald | last post by:
I just started with dotnetnuke, and with a wrong login (wrong password on a clean install i can crash the application pool from IIS. (when i login with the right username/password information the site works fine, i can do anything it is supposed to do) I have the following setup: -Windows 2003 x64 (fully patched, clean install)...
1
5955
by: RMB | last post by:
Environment: VS.Net 2003 FW 1.1 VB.Net I have an application that uses reflection to load external assemblies. The external assemblies reside in the same folder as the application exe. When I run the application as a normal windows application, the external assemblies are loaded just fine. When I run the application as a windows service,...
10
9652
by: =?Utf-8?B?SmFtZXMgV29uZw==?= | last post by:
Hi everybody, I'm trying to use the new VB 2008 right now and I want to know how to preset the company name and copyright informtion in Assembly Information. In my current VB 2005, company name and copyright information (the word "CopyRight" with company name and year) is filled in automatically once a new project is created. However, I...
0
7658
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...
0
7877
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
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...
1
5479
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...
0
5204
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...
0
3631
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...
1
2077
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
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
912
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.