I would like to be able to programmatically install and uninstall my windows service without calling installutil.exe. I've found that calling the Install and Commit methods of the installer doesn't seem to work. I keep getting a NullReferenceException after calling Install. After dissecting InstallUtil, I found that all it really does is take its arguments and forward them to a shared/static method called InstallHelper in the System.Configuration.Install.ManagedInstallerClass class. According to the documentation, "The ManagedInstallerClass type supports the .NET Framework infrastructure and is not intended to be used directly from your code". I tried it anyway, and it works very well.

Does anyone have any suggestions? Is there some way I can directly call the Install and Commit methods without getting errors (maybe there's something I'm missing), or should I just go ahead and call InstallHelper?


Here's the code I was using in a vain attempt to call the methods of the installer myself:
VB Code:
  1. Dim I As New MyInstaller()
  2. Dim H As New Hashtable()
  3. Try
  4.     I.Install(H)
  5.     I.Commit(H)
  6.     MsgBox("success")
  7. Catch ex As Exception
  8.     I.Rollback(H)
  9.     MsgBox("failure")
  10. End Try


Here's the code that works, though the documentation appears to discourage it:
VB Code:
  1. 'just one line
  2. 'it throws an exception if something goes wrong, which is a good thing
  3. System.Configuration.Install.ManagedInstallerClass.InstallHelper(New String() {System.Windows.Forms.Application.ExecutablePath})