I have 3 projects in my VS solution. One of them is a Web app, the second one is a Windows Service and the last one a Setup project for my Web app.
What I want is by the end of the installation of the web app in my setup project, within my custom action to try and install my windows service given that I have the location of the assembly by then.
Ok, here is what REALLY worked for me, it has been tested on multiple machines with different OS ( Vista, XP, Win2k, Win2003 server )
The code has been taken from here so full credit goes to whoever wrote this piece of code.
Once you add the dll or source file into your project make sure to add the ServiceTools namespace and then you have access to some very handy functionality such as...
I hope this helps.
Use below code to install windows service using C#:
Since I was faced with the challenge to install services programmatically which run under a certain user. I extended the
InstallAndStart
method to make use oflp
andlpPassword
...Not much but might help.
In this article I read all posts and comments Bu still I don't know How can I set Account Type and StartType method when I gonna add windows service. This code example works fine my side its just add a service own local system) But I prepare Setup program I must think StartMode and User Account Type method because of customers system.
there is every appearance that ServiceBootFlag enum provide StartType but Account Type still a problem.
After creating an instance of an installer class for my service (very basic) all I had to do is to call :
to install it and
to uninstall the service. The calling code is, here, in the same assembly as the service executable.
to get the service to install through command line all I had to do is to connect this to the executable though command line arguments and test for
System.Environment.UserInteractive
to know if it is the service executing or someone trying to install-uninstall it and voila... no funky interop stuff... no pointers leaking...Total about 20 lines of code spread over two classes did the trick.
to replace InstallUtil just take a look at ManagedInstallerClass.InstallHelper
I found several errors in the code that you reused and have fixed these and also cleaned it up a little. Again, the original code is taken from here.
Please let me know if anyone finds anything wrong with this code!