Installing a Windows Service in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hellen
    New Member
    • Aug 2007
    • 2

    Installing a Windows Service in C#

    Hello,

    I really like your forum and here it came the moment to have an account.
    I have a problem with a Windows Service I want to install to my computer in order to be started automatically at startup. So I have the InstallerClass, all things went fine till the time to use InstallUtil.exe to give the path to my service.

    Do I have the possibility to cal the VS Tool Comand Prompt from the C# code and pass to it smth like : InstallUtil /i C:\Example.exe.

    If not, do you have any idea how to do that from the program and not by the user himself?

    Thanks very much!
  • Pittaman
    New Member
    • Aug 2007
    • 20

    #2
    Hi there (my first post ever here!), I'm not sure what you are trying to do. But if you want to have your webservice as a normal exe which installs itself using InstallUtil you could try and go to the "Program.cs " (by the default) code in which the Service is created and started. It should look something like this:

    Code:
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
            {
    
                ServiceBase[] ServicesToRun;
    
                // More than one user Service may run within the same process. To add
                // another service to this process, change the following line to
                // create a second service object. For example,
                //
                //   ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
                //
                ServicesToRun = new ServiceBase[] { new ADAccountService() };
    
                ServiceBase.Run(ServicesToRun);
            }
    In there you would be able to check for a command line parameter like -i (for install) and then not start the service but execute InstalUtil on the exe itself. So you can run the service executable as a normal program.

    Alltough I think the best Idea would be to use a simple batch script or another exe. A batch file is pretty handy for the users I think. Or is there a reason why that is not an option? Or maybe I'm missing the point.

    Originally posted by Hellen
    Hello,

    I really like your forum and here it came the moment to have an account.
    I have a problem with a Windows Service I want to install to my computer in order to be started automatically at startup. So I have the InstallerClass, all things went fine till the time to use InstallUtil.exe to give the path to my service.

    Do I have the possibility to cal the VS Tool Comand Prompt from the C# code and pass to it smth like : InstallUtil /i C:\Example.exe.

    If not, do you have any idea how to do that from the program and not by the user himself?

    Thanks very much!
    Last edited by Pittaman; Aug 9 '07, 09:14 PM. Reason: Formatting wasn't good enough

    Comment

    • Hellen
      New Member
      • Aug 2007
      • 2

      #3
      Hello,

      Thx for your reply.

      Yes, I have some reasons for not making a batch file...so, I must include the functionality in the program.

      I think I wasn't clear enough in my message. My fault, sorry...

      So I have a solution with more projects. All is done in Windows Forms. I also have an InstallClass where I put what I want the installer to do during setup. One of the solution's program is a monitoring one, so with only one form, invisible for user. This one I want to be started automatically when Windows starts. First I put the path to it in registry's Run key under HKLM. But this allows to the user to kill it using TaskManager or any other program, because the user of the service is the user logged in. For this reason I want that my service be a LocalSystem's service.

      Here is my InstallClass constructor:

      public InstallClass()
      {
      InitializeCompo nent();

      ServiceProcessI nstaller serviceProcessI nstaller = new ServiceProcessI nstaller();
      ServiceInstalle r serviceInstalle r = new ServiceInstalle r();

      //# Service Account Information
      serviceProcessI nstaller.Accoun t = ServiceAccount. LocalSystem;
      serviceProcessI nstaller.Userna me = null;
      serviceProcessI nstaller.Passwo rd = null;

      //# Service Information
      serviceInstalle r.DisplayName = "MonitorService ";
      serviceInstalle r.StartType = ServiceStartMod e.Automatic;

      string appPath = cRegistry.Read( "AppPath");

      //ManagedInstalle rClass.InstallH elper(new string[] { appPath + "Serv.exe"} );

      // This must be identical to the WindowsService. ServiceBase name
      // set in the constructor of WindowsService. cs
      serviceInstalle r.ServiceName = "MyService" ;

      this.Installers .Add(servicePro cessInstaller);
      this.Installers .Add(serviceIns taller);
      }

      But here I can't give the path to my .exe file (by default it puts the path to the dll: "E:\work\Instal l.dll"). I've read that I must use the InstallHelper method of ManagedInstalle rClass. But as you can see I commented her because it didn't work. And finally I've read that I must use InstallUtil.exe but...here is my problem...how to use it in code?


      Any ideas plsss?
      Thx

      Comment

      Working...