0

I am trying to run some custom actions code for windows service (the service is LocalSystem account) with windows service installer and i get the following error message:

error message while installing MSI:

Error 1001. An Exception occurred in the OnAfterInstall event handler
of System.ServiceProcess.ServiceInstaller. --> Access to the path XXX
is denied. 

this code is throwing the error:

protected override void OnAfterInstall(IDictionary savedState)
{
      string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
      System.IO.File.WriteAllText(path, "test");
}

At the code i am trying to access the service .exe directory so i could delete the files created there

My goal is to make a custom action for the install/uninstalling process. i want to delete files that was created after installation like logs and configuration file.

Thanks

Bizhan
  • 16,157
  • 9
  • 63
  • 101
idan
  • 13
  • 1
  • 5
  • please don't use images instead of text. – Bizhan Dec 26 '18 at 14:20
  • please explain... where did you lost me ? i thought it was pretty clear – idan Dec 26 '18 at 14:29
  • It's only 2018 and the technology advancements are not yet ready to process images as fast and convert them to plain text when another user is googling the same error message, nor there are lots of tools available to easily convert screenshots of source codes into text in order for us lazy programmers to copy them in our IDEs. – Bizhan Dec 26 '18 at 14:32

2 Answers2

1

You are trying to write text to a directory instead of a file. The variable "path" is returned from Path.GetDirectoryName() which is a directory. In the next line, you are trying to do File.WriteAllText() to this variable, thus the error.

Aashish Koirala
  • 448
  • 5
  • 12
0

Path.Combine: As already mentioned by others, you need to specify a proper full path (path and file name). Maybe use Path.Combine? For example:

 System.IO.File.WriteAllText(Path.Combine(path, "filename.txt"), "test");

Alternatives: I am not a .NET expert, and I don't use managed code custom actions. However if they are DTF based I am not sure if they have any clunk with regards to current directory or executing directory. Listing some further links:

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164