I implemented a windows form application and would like to run it automatically after installing it as a service. (run the exe file as a service).
In my form, it will be two options (Install the service option and uninstall).
So, my application will be installed as a service, if the user wants to install that as a service, else the user can run it once there is a need for that.
Is this possible to achieve by writing a code in my windows form application project, or it needs to configure a service first by creating a Windows Service project?

Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 16, 2016, 9:09 PM EST
Nishant MittalRank #692 · 3.5K reputationPosted Feb 16, 2016, 8:31 PM EST
Eline Sami47 reputationPosted Feb 16, 2016, 11:41 PM EST
Hello,
I created the Windows service and it runs fine. It can be installed and uninstalled as well.
but how to link the service to the windows form application? In other words. In my Windows form appliciton there is an option to install, but don't know how to call the service and install it. is it possible to share a snippet of code (as an example)?
I found the following, but don't know how to pass the servie name and how to initialize a new instance from the service:
ServiceController sc = new ServiceController(SERVICENAME);
switch (sc.Status)
{
case ServiceControllerStatus.Running:
return "Running";
case ServiceControllerStatus.Stopped:
return "Stopped";
case ServiceControllerStatus.Paused:
return "Paused";
case ServiceControllerStatus.StopPending:
return "Stopping";
case ServiceControllerStatus.StartPending:
return "Starting";
default:
return "Status Changing";
}
private void InstallService()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// Change the following line to match.
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
Eline Sami47 reputationPosted Feb 18, 2016, 1:11 AM EST