Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Start/Stop Window service installed on your machine using System.ServiceProcess in C# .NET 2.0.

Author: Zunnair
Download Source Code : 724_WndSrvsStartstop.zip

: In this simple article you will learn that how you can Start/Stop Window service installed on your machine using System.ServiceProcess Namespace.

Simply just create New Project of windows application in C#.

 

Import ServiceProcess APIs

 

 

using System. ServiceProcess;

 

Drag three labels, three button and one text box on the form.

 

Now write following code on form Load event

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Get Service Status";

            label3.Text = "Get Status";

            textBox1.Text = "ASP.NET State Service";

        }

 

Now write code on Get Status Button It retruns current status of service

 

private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text != "")

            {

                try

                {

                    ServiceController srvs = new ServiceController(textBox1.Text);

                    label3.Text = srvs.Status.ToString();

                    if (srvs.Status == ServiceControllerStatus.Running)

                    {

                        button2.Enabled = false;

                        button3.Enabled = true ;

                    }

                    if (srvs.Status == ServiceControllerStatus.Stopped)

                    {

                        button2.Enabled = true;

                        button3.Enabled = false;

                    }

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                }

            }

            else

                MessageBox.Show("Enter Value");

        }

 

 

Now write code on service start and stop buttons

 

private void button2_Click(object sender, EventArgs e)

        {

        try

        {

            ServiceController srvs = new ServiceController(textBox1.Text);

            srvs.Start();

            button3.Enabled = false;

            button2.Enabled = false;

            label3.Text = "Get Status";

        }

        catch (Exception ex)

        {

            MessageBox.Show(ex.Message);

        }

 

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            try

            {

                ServiceController srvs = new ServiceController(textBox1.Text);

                srvs.Stop();

                button3.Enabled = false;

                button2.Enabled = false;

                label3.Text = "Get Status";

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

Here srvs.start() function start window service and srvs.Stop() function stops service.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Get status of Window service installed on your machine using System.ServiceProcess in C# .NET 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net