InvalidCastException while changing windows service startup type
-
Hi all, I am using the following code to change startup type of a windows service. string MyserviceName = "Test Service1"; ServiceController servicen = new ServiceController(MyserviceName); string ServiceName = servicen.ServiceName.ToString(); string startupType = "Automatic"; 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); } But, I am getting a InvalidCastException as "Specified cast is not valid" error at service.GetMethodParameters("ChangeStartMode"); Please help me.
-
Hi all, I am using the following code to change startup type of a windows service. string MyserviceName = "Test Service1"; ServiceController servicen = new ServiceController(MyserviceName); string ServiceName = servicen.ServiceName.ToString(); string startupType = "Automatic"; 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); } But, I am getting a InvalidCastException as "Specified cast is not valid" error at service.GetMethodParameters("ChangeStartMode"); Please help me.
Odd. I see nothing wrong with that line of code. Are you certain the exception is at that line? /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com