I created a batch file for installing service because I need to install my service on PC don't have Visual Studio.
Content of batch file:
@echo OFF
echo Installing service...
sc create "MyService" binpath= %~dp0\MyService.exe start= auto
echo Installing service complete
pause
And I need to autostart MyService after install, so I make this code:
private void svInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController(svInstaller.ServiceName);
sc.Start();
}
Don't any problem if I installing my service by Visual Studio Command Prompt with InstallUtil. When I install service by batch file, my service didn't autostart.
How to possible to auto start my service after install by batch file?
Update: Thanks Sam Denty's answer, I problem is resolved.
But I have another question: When I install my service by sc, my code in AfterInstall function do not work?