Well, just a small issue, I'm using the code below to start and stop a particular service, but after a restart it will return to it's original state. Is it best I edit the Registry also, when stopping or starting the service... Surely when you stop a service it's going to stay disabled/stopped until the user starts it again? Code: Public Sub sscertprop() Dim cp As ServiceController = New ServiceController("CertPropSvc") If (cp.Status.Equals(ServiceControllerStatus.Stopped)) Or (cp.Status.Equals(ServiceControllerStatus.StopPending)) Then cp.Start() Else cp.Stop() End If End Sub I use the code by simply calling it.
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();
Sorry about not being clear =/ Basically, I want to completely stop/disable a service until the user starts it again in the application, but am also giving the user the option to enable it at a later date. Say the user removes their wireless card and use LAN only, they'd want to disable the WLAN AutoConfig service (Wlansvc) and the code in the OP would stop a service but it would start after a computer reboot, or they'd start a service and it would stop after a reboot. Anyway this code seems to have solved it... Code: Public Sub sscertprop() 'Select the particular service Dim cp As ServiceController = New ServiceController("CertPropSvc") If (cp.Status.Equals(ServiceControllerStatus.Stopped)) Or (cp.Status.Equals(ServiceControllerStatus.StopPending)) Then 'Set startup to automatic My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\CertPropSvc", "Start", "2", RegistryValueKind.DWord) 'Start the service cp.Start() Else 'Set the startup to manual My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\CertPropSvc", "Start", "3", RegistryValueKind.DWord) 'Stop the service cp.Stop() End If End Sub Unless anyone can think of a better option this has worked perfectly so far.
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
Anything to disable startup items? I can list them ok, having a hard time finding a good way to disable them.
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.
Actually, good idea, right now I'm reading application location and displaying information via the registry key.
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