locked
How to start a WINDOWS SERVICE from a WINDOWS FORM RRS feed

  • Question

  • Hi, I have a windows service project, and a windows forms project (every one in a different  solution). So, to  start my windows service afterward it is installed, i must to put in a system console ( CMD ):  "NET START serviceName"  ( or "NET STOP..") and the service starts running.

    Then, i want to run my service from my windows form program. If i click the button "Run service" implemented in my program, this would do the same thing that when i put in CMD the command "NET START nameService".

    is it possible?

    thanks in advance (and sorry about my english, Im from spain).


    Wednesday, August 31, 2011 4:16 PM

Answers

  • Ofcourse it is possible,

    Here is the code,

    string serviceName = "SomeService";
    ProcessStartInfo startInfo = new ProcessStartInfo("NET", "START " + serviceName);
    startInfo.CreateNoWindow = true;
    Process.Start(startInfo);
    

    You can also use SC.exe instead of NET.exe.

    string serviceName = "SomeService";
    ProcessStartInfo startInfo = new ProcessStartInfo("SC", "START " + serviceName);
    startInfo.CreateNoWindow = true;
    Process.Start(startInfo);
    

    Hope this helps you.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by Kris Anderson Friday, September 2, 2011 1:26 PM
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 6:24 PM
    • Proposed as answer by Link.fr Tuesday, September 6, 2011 9:09 PM
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 6:57 PM
  • If you want to use a Console beside a win form, you can do it this way:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Globalization;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    
    namespace Avg30_1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          Win32.AllocConsole();
          Console.WriteLine("CMD: Service started");
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          Win32.AllocConsole();
          Console.WriteLine("CMD: Service stopped");
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          Win32.AllocConsole();
          Console.WriteLine(String.Format("{0} pressed", sender));
        }
      }
    
      public class Win32
      {
        /// <summary>
        /// Allocates a new console for current process.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
    
        /// <summary>
        /// Frees the console.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
      }
    }
    
    



    Mitja
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 4:57 PM
  • Hi

    You can do by using WMI

    and these resources can give code snippets

    http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=114

    http://www.serverwatch.com/tutorials/article.php/1576131/Windows-Services-Management-With-WMI-Part-1.htm

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 4:58 PM
  • Hi again. All your answer helped me very much. 

    It finnally was easier than expected, maybe it can to be useful to anybody:

    ----

           ServiceController servicio1 = new ServiceController("ServiceName"):    

     

    //and the next button start service when it is stopped, or stop service when it is running

     private void button1_Click(object sender, EventArgs e)
     {
           if (servicio1.Status == ServiceControllerStatus.Running)
           {
              servicio1.Stop();
              servicio1.WaitForStatus(ServiceControllerStatus.Stopped); //Para que no continúe hasta que se haya parado completamente
                   
           }
           else
           {
               servicio1.Start();
               servicio1.WaitForStatus(ServiceControllerStatus.Running);  
           }

     }

    ----

     



    • Edited by SankB Tuesday, September 6, 2011 4:00 PM
    • Proposed as answer by Link.fr Tuesday, September 6, 2011 9:09 PM
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Tuesday, September 6, 2011 3:58 PM

All replies

  • If you want to use a Console beside a win form, you can do it this way:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Globalization;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    
    namespace Avg30_1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          Win32.AllocConsole();
          Console.WriteLine("CMD: Service started");
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
          Win32.AllocConsole();
          Console.WriteLine("CMD: Service stopped");
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          Win32.AllocConsole();
          Console.WriteLine(String.Format("{0} pressed", sender));
        }
      }
    
      public class Win32
      {
        /// <summary>
        /// Allocates a new console for current process.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
    
        /// <summary>
        /// Frees the console.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
      }
    }
    
    



    Mitja
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 4:57 PM
  • Hi

    You can do by using WMI

    and these resources can give code snippets

    http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=114

    http://www.serverwatch.com/tutorials/article.php/1576131/Windows-Services-Management-With-WMI-Part-1.htm

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Wednesday, August 31, 2011 4:58 PM
  • Hi

    You can do by using WMI

    and these resources can give code snippets

    http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=114

    http://www.serverwatch.com/tutorials/article.php/1576131/Windows-Services-Management-With-WMI-Part-1.htm

     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 4:58 PM
  • Ofcourse it is possible,

    Here is the code,

    string serviceName = "SomeService";
    ProcessStartInfo startInfo = new ProcessStartInfo("NET", "START " + serviceName);
    startInfo.CreateNoWindow = true;
    
    Process.Start(startInfo);
    


    You can also use SC.exe instead of NET.exe.

    string serviceName = "SomeService";
    ProcessStartInfo startInfo = new ProcessStartInfo("SC", "START " + serviceName);
    startInfo.CreateNoWindow = true;
    
    Process.Start(startInfo);
    

    Hope this helps you.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Wednesday, August 31, 2011 6:16 PM
  • Ofcourse it is possible,

    Here is the code,

    string serviceName = "SomeService";
    ProcessStartInfo startInfo = new ProcessStartInfo("NET", "START " + serviceName);
    startInfo.CreateNoWindow = true;
    Process.Start(startInfo);
    

    You can also use SC.exe instead of NET.exe.

    string serviceName = "SomeService";
    ProcessStartInfo startInfo = new ProcessStartInfo("SC", "START " + serviceName);
    startInfo.CreateNoWindow = true;
    Process.Start(startInfo);
    

    Hope this helps you.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by Kris Anderson Friday, September 2, 2011 1:26 PM
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 6:24 PM
    • Proposed as answer by Link.fr Tuesday, September 6, 2011 9:09 PM
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Wednesday, August 31, 2011 6:57 PM
  • Hi again. All your answer helped me very much. 

    It finnally was easier than expected, maybe it can to be useful to anybody:

    ----

           ServiceController servicio1 = new ServiceController("ServiceName"):    

     

    //and the next button start service when it is stopped, or stop service when it is running

     private void button1_Click(object sender, EventArgs e)
     {
           if (servicio1.Status == ServiceControllerStatus.Running)
           {
              servicio1.Stop();
              servicio1.WaitForStatus(ServiceControllerStatus.Stopped); //Para que no continúe hasta que se haya parado completamente
                   
           }
           else
           {
               servicio1.Start();
               servicio1.WaitForStatus(ServiceControllerStatus.Running);  
           }

     }

    ----

     



    • Edited by SankB Tuesday, September 6, 2011 4:00 PM
    • Proposed as answer by Link.fr Tuesday, September 6, 2011 9:09 PM
    • Marked as answer by Leo Liu - MSFT Thursday, September 8, 2011 3:23 AM
    Tuesday, September 6, 2011 3:58 PM