title: Start Windows Service in C#
body: <p>I want to start a windows service that was just installed.</p>
  
  <pre><code>ServiceBase[] ServicesToRun;
  if (bool.Parse(System.Configuration.ConfigurationManager.AppSettings["RunService"]))
  {
      ServicesToRun = new ServiceBase[] { new IvrService() };
      ServiceBase.Run(ServicesToRun);
  }
  </code></pre>
  
  <p>The IvrService code is:</p>
  
  <pre><code>partial class IvrService : ServiceBase
  {
      public IvrService()
      {
          InitializeComponent();
  
          Process myProcess;
          myProcess = System.Diagnostics.Process.GetCurrentProcess();
  
          string pathname = Path.GetDirectoryName(myProcess.MainModule.FileName);
  
          //eventLog1.WriteEntry(pathname);
  
          Directory.SetCurrentDirectory(pathname);
  
      }
  
      protected override void OnStart(string[] args)
      {
          string sProcessName = Process.GetCurrentProcess().ProcessName;
  
          if (Environment.UserInteractive)
          {
              if (sProcessName.ToLower() != "services.exe")
              {
                  // In an interactive session.
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  Application.Run(new IvrInteractive());
                  IvrApplication.Start(); // the key function of the service, start it here
                  return;
              }
          }
      }
  </code></pre>
  
  <p>I am not sure how to start the service. Using <code>ServiceController.Start()?</code> But I already have <code>ServiceBase.Run(ServicesToRun);</code> Is it for starting a service?</p>
  
  <p>Code hint is definitely appreciated.</p>