How do I change a Windows Service's startup type in .NET (post-install)?

how to change path to executable in services windows 10
delete windows service
powershell get-service path to executable
create windows service
windows service paths
edit service windows server
edit windows service properties
sc config binpath quotes

I have a program that installs a service, and I'd like to be able to give the user the option later on to change the startup type to "Automatic".

The OS is XP - if it makes any difference (Windows APIs?).

How can I do this in .NET? C# if possible! :)

You can use the OpenService() and ChangeServiceConfig() native Win32 APIs for that purpose. I believe that there is some information on pinvoke.net and of course on MSDN. You might want to check out the P/Invoke Interopt Assistant.

Start, Stop, and Disable Services in Windows 10, By going to Administrative Tools > Services you can open a properties dialog and view the Path to executable , but there is no way to change it. Is  In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button. On the menu bar, choose Attach to Process from the Debug or Tools menu. (Keyboard: Ctrl+Alt+P) The Processes dialog box appears. Select the Show processes from all users check box.

I wrote a blog post on how to do this using P/Invoke. Using the ServiceHelper class from my post you can do the following to change the Start Mode.

var svc = new ServiceController("ServiceNameGoesHere");  
ServiceHelper.ChangeStartMode(svc, ServiceStartMode.Automatic); 

shortcut, close Administrative Tools, and go to step 4 below. Change active hours: If Windows is restarting for updates when you're using your computer, you can adjust this setting to tell Windows when you're on your computer, to make sure that updates don't force-restart during that time. Pick the automatic option and Windows will choose the active hours based on your activity.

In the service installer you have to say

[RunInstaller(true)]
public class ProjectInstaller : System.Configuration.Install.Installer 
{
    public ProjectInstaller()
    {
        ...
        this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
    }
}

You could also ask the user during installation and then set this value. Or just set this property in the visual studio designer.

By default, you cannot directly change the path to executable in the Windows service program. Please follow below steps if you want to change  Type Services in Windows Find MongoDB (or the service you want to change) and open the service, making sure to stop it. Make a note of the Service Name (not the display name) Look up and copy the "Path to executable" and copy it.

You can use WMI to query all services and then match the service name to the inputted user value

Once the service has been found just change the StartMode Property

if(service.Properties["Name"].Value.ToString() == userInputValue)
{
    service.Properties["StartMode"].Value = "Automatic";
    //service.Properties["StartMode"].Value = "Manual";
}

//This will get all of the Services running on a Domain Computer and change the "Apple Mobile Device" Service to the StartMode of Automatic.  These two functions should obviously be separated, but it is simple to change a service start mode after installation using WMI

private void getServicesForDomainComputer(string computerName)
{
    ConnectionOptions co1 = new ConnectionOptions();
    co1.Impersonation = ImpersonationLevel.Impersonate;
    //this query could also be: ("select * from Win32_Service where name = '" + serviceName + "'");
    ManagementScope scope = new ManagementScope(@"\\" + computerName + @"\root\cimv2");
    scope.Options = co1;

    SelectQuery query = new SelectQuery("select * from Win32_Service");

    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
    {
        ManagementObjectCollection collection = searcher.Get();

        foreach (ManagementObject service in collection)
        {
            //the following are all of the available properties 
            //boolean AcceptPause
            //boolean AcceptStop
            //string Caption
            //uint32 CheckPoint
            //string CreationClassName
            //string Description
            //boolean DesktopInteract
            //string DisplayName
            //string ErrorControl
            //uint32 ExitCode;
            //datetime InstallDate;
            //string Name
            //string PathName
            //uint32 ProcessId
            //uint32 ServiceSpecificExitCode
            //string ServiceType
            //boolean Started
            //string StartMode
            //string StartName
            //string State
            //string Status
            //string SystemCreationClassName
            //string SystemName;
            //uint32 TagId;
            //uint32 WaitHint;
            if(service.Properties["Name"].Value.ToString() == "Apple Mobile Device")
            {
                service.Properties["StartMode"].Value = "Automatic";
            }
        }
    }         
}

I wanted to improve this response... One method to change startMode for Specified computer, service:

public void changeServiceStartMode(string hostname, string serviceName, string startMode)
{
    try
    {
        ManagementObject classInstance = 
            new ManagementObject(@"\\" + hostname + @"\root\cimv2",
                                 "Win32_Service.Name='" + serviceName + "'",
                                 null);

        // Obtain in-parameters for the method
        ManagementBaseObject inParams = classInstance.GetMethodParameters("ChangeStartMode");

        // Add the input parameters.
        inParams["StartMode"] = startMode;

        // Execute the method and obtain the return values.
        ManagementBaseObject outParams = classInstance.InvokeMethod("ChangeStartMode", inParams, null);

        // List outParams
        //Console.WriteLine("Out parameters:");
        //richTextBox1.AppendText(DateTime.Now.ToString() + ": ReturnValue: " + outParams["ReturnValue"]);
    }
    catch (ManagementException err)
    {
        //richTextBox1.AppendText(DateTime.Now.ToString() + ": An error occurred while trying to execute the WMI method: " + err.Message);
    }
}

Today I've been editing and testing a Windows Service application. I've got To change the location of the exe just change the ImagePath key. Windows service applications run in their own security context and are started before the user logs into the Windows computer on which they are installed. You should plan carefully what user account to run the service within; a service running under the system account has more permissions and privileges than a user account.

In ProjectInstaller.cs, click/select the Service1 component on the design surface. In the properties windo there is a startType property for you to set this.

Learn how to change service configurations using the sc.exe utility. a service will run, or specifies a name of the Windows driver object in  To do this, you’ll need to run the app as a Windows service. Windows services are a special class of programs that are configured to launch and run in the background, usually without any sort of user interface and without needing a user to log in to the PC.

This example shows how to change a service's startup type. service is disabled​, then any applications that depend on BITS, such as Windows Update or MSN  Here is how you can remove a redundant service. To delete a service in Windows 10, do the following. First of all, you need to find the exact service name. Here is the fast method to find it. Press Win + X shortcut keys on the keyboard to open the Power user menu. Alternatively, you can right-click on the Start menu. Tip: You can tweak and

Changing or updating the value "Path to executable" in the Service's properties window is as easy as modifying the value in the local registry. You Hate Your Windows, So Why Wait? Get Your Free Replacement Window Quote Now! 1:Enter Zip Code - 2:Select Options - 3:Get Replacement Window Price Quotes!

To change the executable path of ServiceDesk go to below-mentioned location from registry. run -> regedit -> navigate to the below-mentioned location and  With Find Your Windows, We Make It Easy To Find Replacement Windows In Your Local Area. Window Replacement Made Easy with Find Your Windows. Get 4 Free Quotes Instantly.

Comments
  • Downvoter: it would be helpful if you'd care to explain.
  • I tried this out in a few VMs, and everything seems to work well in Win 7. Thanks again.
  • @PeterKelly In blog you mentioned "There is a managed option in .NET - the System.ServiceProcess.ServiceController class which exposes some useful Properties and Methods for managing a Service. However, it provides no way to change the start mode of a Service." Why is it so? I mean why was this functionality left out? Also can this be invoked on remote machine like .NET's ServiceController takes MachineName?
  • ServiceHelper appears to be deprecated now =(
  • @Dave ServiceHelper is a custom class I wrote in my blog post. Saying it is deprecated doesn't make sense! :)
  • One more thing... if you want to change the service on a different computer, then set the MachineName on the ServiceController class, then change ChangeStartMode method to the following: public static void ChangeStartMode(ServiceController svc, ServiceStartMode mode) { string machineName = string.IsNullOrWhiteSpace(svc.MachineName) ? null : svc.MachineName; var scManagerHandle = OpenSCManager(machineName, null, SC_MANAGER_ALL_ACCESS); ...
  • Ah OK, so it isn't possible post install without uninstalling the service?
  • You want to do that post install? Then you have to use WMI.
  • Modifying the registry would be the best way to change the startup type post-install. You can use the Microsoft.Win32.Registry class to do it.
  • Don't do that. Hacks like these are exactly the reason why there is tons of compatibility stuff in newer windows versions. There are perfectly reasonable APIs to do it - just that they are not managed. However when you know you are on windows (you know in both cases Registry or API/unmanged), that shouldn't matter.
  • How is using the registry the way it is meant to be used a hack? The API modifies the registry, its just a layer of abstraction...
  • Well, if they ever need or want to change the (logical) layout of the registry for service registration, your code will break. So unless that part of the registry is explicitly document, and I doubt it is, it should be off-limits (aside manual troubleshooting, maybe). Besides, are you sure that the service control manager will actually recognize changes you make directly to the registry vs. those done via the official API? There might be a whole boatload of issues that only surface when you don't expect them. Just don't go down that road.
  • Well seeing that the API changes that registry value I'd say that the changes you are suggesting would break my code would also break the API. Did you even read the API docs you linked to? Because they mention the use of the registry...