Not understanding the question. Do you wish to disable it from starting after a restart? Take a look at: Code: technet.microsoft.com/en-us/library/cc990290(v=ws.10).aspx using start=disabled
Dave your question is some what confusing, you don't need to touch registry if stopping or starting a service.. Do you want to disable the service from starting ? If so i would use registry, maybe something like: Code: //2 = Automatic //3 = Manual //4 = Disabled RegistryKey ss = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\ServiceName", true); ss.SetValue("Start", 4); ss.Close();
then what would you use? ServiceController provides better error handling than shellexec lol. I use SC.exe just to install my services then ServiceController to do the rest.
Yeh I get like that from time to time Sarcy.. I do use a combination of both depending on what im doing..
lol Alpha... I think that WMI could stop and disable it not sure though.. i will look into it EDIT: wmi would do it better. Code: ' Lock to Your service - Mine is Reboot-To Updater service Dim classInstance As New ManagementObject( _ "root\CIMV2", _ "Win32_Service.Name='SF-Reboot-To'", _ Nothing) Dim outParams As ManagementBaseObject = _ classInstance.InvokeMethod("StopService", Nothing, Nothing) ' List outParams Console.WriteLine("Out parameters:") Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue")) ' Obtain [in] parameters for the method Dim DisabledParam As ManagementBaseObject = _ classInstance.GetMethodParameters("ChangeStartMode") ' Add the input parameters. DisabledParam("StartMode") = "Disabled" ' Execute the method and obtain the return values. Dim outDisable As ManagementBaseObject = _ classInstance.InvokeMethod("ChangeStartMode", DisabledParam, Nothing) ' List outParams Console.WriteLine("Out parameters:") Console.WriteLine("ReturnValue: {0}", outDisable("ReturnValue"))
it gets even better once you start using management class gen http://msdn.microsoft.com/en-us/library/2wkebaxa(v=VS.80).aspx
dump the reg item that you are disabling then delete the reg value. for services make a list of them and disable the ones that you want and store a list in the registry or a file on the hdd.
PMR already done it for you: Code: Dim classInstance As New ManagementObject("root\CIMV2", "Win32_Service.Name='ServiceName'", Nothing) Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("StopService", Nothing, Nothing) Dim DisabledParam As ManagementBaseObject = classInstance.GetMethodParameters("ChangeStartMode") DisabledParam("StartMode") = "Disabled" Dim outDisable As ManagementBaseObject = classInstance.InvokeMethod("ChangeStartMode", DisabledParam, Nothing) Another crappy way: Code: For Each obj In GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select State from Win32_Service where Name = 'ServiceName'") If obj.State = "Running" Then obj.StopService() End If obj.ChangeStartMode("Disabled") Next