Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. InvalidCastException while changing windows service startup type

InvalidCastException while changing windows service startup type

Scheduled Pinned Locked Moved C#
helpsysadminworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AshwiniSH
    wrote on last edited by
    #1

    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.

    R 1 Reply Last reply
    0
    • A AshwiniSH

      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.

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups