I have a Windows Service project in Visual Studio in C #, however, I need to install this project from lines of code, without using #
Is there any way to do this?
I have a Windows Service project in Visual Studio in C #, however, I need to install this project from lines of code, without using #
Is there any way to do this?
You can use the ManagedInstallerClass
(responsible for dealing with the Installutil ), more specifically, the method InstallHelper
:
static void Main(string[] args) {
if (System.Environment.UserInteractive) {
if (args.Length > 0) {
switch (args[0]) {
case "-install": {
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
}
case "-uninstall": {
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
}
}
else {
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyService() };
ServiceBase.Run(ServicesToRun);
}
}
Example usage:
meuProjeto.exe -install
meuProjeto.exe -uninstall