Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am developed a C# windows application through which I am able to install a service in windows 7 but not able to run the service. Please help.
Posted
Updated 2-Dec-15 23:34pm
v2
Comments
[no name] 3-Dec-15 6:44am    
Please go through below URL:

http://www.c-sharpcorner.com/uploadfile/mahesh/servicecontroller-in-C-Sharp/

Here's a bit of code I have handy:

C#
public string Start(string serviceName)
{
    using (ServiceController serviceController = new ServiceController(serviceName))
    {
        try
        {
            serviceController.Start();
            serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(30));
            return "+ ok";
        }
        catch (Exception ex)
        {
            return "- " + ex.Message;
        }
    }
}


Is that what you want?
 
Share this answer
 
Comments
IndrajitDasgupat 3-Dec-15 5:54am    
Yes I am working like that but service is not started
Rob Philpott 3-Dec-15 6:30am    
ok, is it disabled. Have a look in the services control manager. It will be disabled/manual/automatic or potentially 'automatic delayed'. It won't start if disabled. Check the event log, if there was a problem starting it may well appear in that.
Try out the below code hope it will help you out

C#
public void StartService(string ServiceName)
{
	//Add namespace using System.ServiceProcess; for using ServiceController class
	ServiceController myService = new ServiceController();
	myService.ServiceName =ServiceName;
	string svcStatus = myService.Status.ToString();
	if (svcStatus == "Stopped")
            {
                myService.Start();
                string svcStatusWas = "";
                while (svcStatus != "Running")
                {
                    svcStatusWas = svcStatus;
                    myService.Refresh();
                    svcStatus = myService.Status.ToString();
                }
                //Service Started
            }
            else
            {
                // STOP the service if it is in any other state
                myService.Stop();
                while (svcStatus != "Stopped")
                {
                    myService.Refresh();
                    svcStatus = myService.Status.ToString();
                }
		myService.Start();
                string svcStatusWas = "";
                while (svcStatus != "Running")
                {
                    svcStatusWas = svcStatus;
                    myService.Refresh();
                    svcStatus = myService.Status.ToString();
                }
		//Service Started
            }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900