Install Windows Service - Current Directory

This question is related to my previous one . I wrote a service in C # and I need to make it dynamic and load the name from the configuration file. The problem is that the current directory when calling the service installer is the net framework 4 directory, and not the one in which my assembly is located.

Using the line (which helps with the same problem, but the service is already running) System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

sets the directory to

 C:\Windows\Microsoft.NET\Framework\v4.0.30319 

which was also the initial value.

How to get the right path?

0
source share
3 answers

try the following:

 Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
+7
source

You can also try

 Assembly.GetExecutingAssembly( ).Location 

This also works if you are not referencing winforms or wpf

+3
source

We had the same problem in the project that I was working on, but we used a different approach. Instead of using the App.config files, which should be in the same path as the executable, we changed both the installer class and the main service entry point.

We did this because we do not need the same project files in different places. The idea was to use the same distribution files, but with different service names.

So what we did was inside our ProjectInstaller:

  private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e) { string keyPath = @"SYSTEM\CurrentControlSet\Services\" + this.serviceInstaller1.ServiceName; RegistryKey ckey = Registry.LocalMachine.OpenSubKey(keyPath, true); // Pass the service name as a parameter to the service executable if (ckey != null && ckey.GetValue("ImagePath")!= null) ckey.SetValue("ImagePath", (string)ckey.GetValue("ImagePath") + " " + this.serviceInstaller1.ServiceName); } private void ProjectInstaller_BeforeInstall(object sender, InstallEventArgs e) { // Configura ServiceName e DisplayName if (!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"])) { this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"]; this.serviceInstaller1.DisplayName = this.Context.Parameters["ServiceName"]; } } private void ProjectInstaller_BeforeUninstall(object sender, InstallEventArgs e) { if (!String.IsNullOrEmpty(this.Context.Parameters["ServiceName"])) this.serviceInstaller1.ServiceName = this.Context.Parameters["ServiceName"]; } 

We used InstallUtil to install our service as follows:

[FramerokPath]\installutil /ServiceName=[name] [ExeServicePath]

Then, inside the Main entry point of your application, we checked the args attribute to get what was the name of the service installation that we installed inside the AfterInstall event.

This approach has some problems, for example:

  • We needed to create a default name for a service that was installed without a parameter. For example, if the name was not passed to our service, we use the default value:
  • You can change the service name passed to our application to be different from the one that was installed.
+1
source

All Articles