how obtain the exe path of a window service file?

  • Thread starter Thread starter Willie wjb
  • Start date Start date
W

Willie wjb

Hi,

i created a windows service. This service is installed in some c:\program
files\x directory
when started it needs the directory in which it is started.

when i use the system.environment.currentdirectory i get the
C:\WINNT\SYSTEM32 in stead of the
c:\program files\x.

i think the windows service is started by another program out of the
system32 dir.
Is there a (simple) way to get the c:\program files\x direcotry where the
executable is located.

thanks.
 
In short, use:

System.Reflection.Assembly.GetEntryAssembly.Location

See my post "Re: AppPath() Replacement" for more information

Hope this helps,

Trev.
 
You're absolutely right! Your service is started by the
system service manager.

Try using System.IO.Directory.GetCurrentDirectory, this
should return your applications working directory and not
C:\WINNT\SYSTEM32.

-Fabricio
 
Try using System.IO.Directory.GetCurrentDirectory, this
should return your applications working directory and not
C:\WINNT\SYSTEM32.

Nope, Sorry. It does the same as Environment.CurrentDirectory. Try starting
an app from a shortcut that has its "Start In" property set to some other
directory.

If you look at the IL using IL DSM, you'll see that
Environment.CurrentDirectory actually calls System.IO.GetCurrentDirectory.

Use System.Reflection.Assembly.GetEntryAssembly.Location to get the full
path (including filename) of the directory.

Hope this helps,

Trev.
 
* "Willie wjb said:
i created a windows service. This service is installed in some c:\program
files\x directory
when started it needs the directory in which it is started.

when i use the system.environment.currentdirectory i get the
C:\WINNT\SYSTEM32 in stead of the
c:\program files\x.

i think the windows service is started by another program out of the
system32 dir.
Is there a (simple) way to get the c:\program files\x direcotry where the
executable is located.

\\\
Imports System.IO
Imports System.Reflection
..
..
..
Private Function ApplicationPath() As String
Return _
Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)
End Function
///
 
Herfried,

Cheers for the tip about the path object. I learn something new every day ;)
Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)

Don't forget that GetExecutingAssembly.Location returns the location of the
Assembly that is excuting (which could be a DLL in another location than the
main exe). IMHO, GetEntryAssembly.Location is a better bet.

Trev.

Herfried K. Wagner said:
* "Willie wjb said:
i created a windows service. This service is installed in some c:\program
files\x directory
when started it needs the directory in which it is started.

when i use the system.environment.currentdirectory i get the
C:\WINNT\SYSTEM32 in stead of the
c:\program files\x.

i think the windows service is started by another program out of the
system32 dir.
Is there a (simple) way to get the c:\program files\x direcotry where the
executable is located.

\\\
Imports System.IO
Imports System.Reflection
.
.
.
Private Function ApplicationPath() As String
Return _
Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)
End Function
///
 
this works fine!!!

thanks.

Codemonkey said:
In short, use:

System.Reflection.Assembly.GetEntryAssembly.Location

See my post "Re: AppPath() Replacement" for more information

Hope this helps,

Trev.
 
Back
Top