C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to start and stop windows services programmatically


Posted Date: 20 Apr 2005    Resource Type: Articles    Category: .NET Framework

Posted By: Tony John       Member Level: Gold
Rating:     Points: 10



Introduction



This short code snippet shows how you can start and stop windows services and also how to find the current status of any windows services.

System.ServiceProcess.ServiceController class



You can use the .NET class System.ServiceProcess.ServiceController to work with the windows services.

Include the namespace in the top of your class:

VB.NET

Imports System.ServiceProcess

C#

using System.ServiceProcess;


Create an instance of the class:
VB.NET

dim controller as new ServiceController

C#

ServiceController controller = new ServiceController();


See the following code which gets the status of the IIS Admin service. Also, you can start and stop the service using this class.

VB.NET

controller.MachineName = "."
controller.ServiceName = "IISADMIN"
dim status as string = controller.Status.ToString

' Stop the service
controller.Stop()

' Start the service
controller.Start()

C#

controller.MachineName = ".";
controller.ServiceName = "IISADMIN";
string status = controller.Status.ToString();

// Stop the service
controller.Stop();

// Start the service
controller.Start();





Responses

Author: Yuri Sho    22 Apr 2008Member Level: Bronze   Points : 0
When I try happen this error:

System.InvalidOperationException = {"Cannot stop wuauserv service on computer '.'."}

InnerException = {"The service has not been started"}

System.ComponentModel.Win32Exception = {"The service has not been started"}

ErrorCode = -2147467259

NativeErrorCode = 1062

How to correct? ThX!


Author: Vikas Kumar Singh    14 May 2008Member Level: Bronze   Points : 2
Hi,

Check for the service's startup type if disable. Keep it either Automatic or Manual.

Regards,
Vikas Singh


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Thanks a Lot!  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Examples of how to Calculate Different VB.NET Dates
Previous Resource: How to schedule a task using windows Services
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use