Skip to content

Windows Service Start Mode

zsafadi

Asked Apr 12, 2005, 3:30 AM EDT4 answers
Share
LinkedInX
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.

  • 0
    Hi You can change the startup type from the Services Control Manager...Control Panel--->Administrative Tools--->Services..then right click your service ..properties..then change the startup type Good luck
    0
  • 0
    hey man sure i can do it from there, but i need to do it in code!!! C#.Net
    0
  • 0
    this code will be useful to u......
    TypeScript
    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);
    
             }
            }
    
    
    
    }
    0
  • 0

    Thank you MuthuPandi for the code It helped me a lot

    0