Windows Service Start Mode
hi all,
i created a windows service and build a controller to that service, just as the SQL Server Service Manager if any saw it. Anyway the problem is i want to control the startup mode of the windows service (Automatic, Manual, Disabled), in .Net you can specify this in the Service Installer but after that i didn't find a way to change it.
Does anyone know a way to change the startup mode of the Windows Service in .Net? i think that i foud a way to do it in win32, but i need it in .Net...
thanks for help
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
MohammadShall44 pointsPosted Apr 12, 2005, 4:39 AM EDT
zsafadi40 pointsPosted Apr 12, 2005, 9:42 PM EDT
muthu pandi2 pointsPosted Jan 27, 2011, 1:12 AM EST
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.DirectoryServices; using System.Diagnostics; using System.ServiceProcess; using System.Configuration.Install; using System.Management; using System.Windows.Forms; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string serviceName = TextBox1.Text; ServiceController service = new ServiceController(serviceName); if (service.Status != ServiceControllerStatus.Running) { // do something service.Start(); } } protected void Button2_Click(object sender, EventArgs e) { string serviceName = TextBox1.Text; ServiceController service = new ServiceController(serviceName); if (service.Status == ServiceControllerStatus.Running) { // do something service.Stop(); } } protected void Button3_Click(object sender, EventArgs e) { string serviceName = TextBox1.Text; ServiceController servicen = new ServiceController(serviceName); string ServiceName = servicen.ServiceName.ToString(); string startupType = DropDownList1.SelectedValue.ToString(); ManagementPath myPath = new ManagementPath(); ManagementBaseObject outParams = null; myPath.Server = System.Environment.MachineName; myPath.NamespacePath = @"root\CIMV2"; myPath.RelativePath = "Win32_Service.Name='" + ServiceName + "'"; using (ManagementObject service = new ManagementObject(myPath)) { // Set startmode to Automatic (auto start at boot ) ManagementBaseObject inputArgs = service.GetMethodParameters("ChangeStartMode"); inputArgs["startmode"] = startupType; outParams = service.InvokeMethod("ChangeStartMode", inputArgs, null); } } }Ashwini S3 pointsPosted Apr 7, 2014, 3:27 AM EDT
Thank you MuthuPandi for the code It helped me a lot