How to start and stop the any window service in the windows operating system using windows app

coder rock 296 Reputation points
2021-07-07T17:22:30.38+00:00

I am trying to building generic tool from which we can control any windows service in the window operating system using asp.net window app, i got this references: https://stackoverflow.com/questions/19094712/control-the-windows-service i have added two buttons start and stop from below code, but i am getting the error in exception: {"Cannot start service aspnet_state on computer '.'."}.

   public Form3()
    {
        InitializeComponent();
    }

    private void start_Click(object sender, EventArgs e)
    {

        string serviceName = "aspnet_state";
        double timeoutMilliseconds = 10_000;
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

        ServiceController service = new ServiceController(serviceName);
        try
        {
            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }
        catch (InvalidOperationException exc)
        {
            MessageBox.Show($"The service failed to start in a timely manner\n\n{exc}");
        }
    }

    private void stop_Click(object sender, EventArgs e)
    {
        string serviceName = "aspnet_state";
        double timeoutMilliseconds = 10_000;
        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

        ServiceController service = new ServiceController(serviceName);
        try
        {
            service.Stop();
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
        }
        catch (InvalidOperationException exc)
        {
            MessageBox.Show($"The service failed to start in a timely manner\n\n{exc}");
        }
    }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,885 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,849 questions
{count} votes

Accepted answer
  1. coder rock 296 Reputation points
    2021-09-21T19:13:24.867+00:00

    Hi,

    I have debug my code is working correctly only the issue was service name was wrong.

    Regards,
    Mahzar Khan

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-07-08T08:40:37.83+00:00

    Hi mohdmazharkhan-1078,
    Try the following suggestions:

    1. Right click on the application and click Run as administrator.
    2. Run Visual Studio as an administrator.
    3. Go to c://Program Files/ApplicationFolder/.exe Right click on .exe and go to Properties then go Compability Tab and check true to Run this Program as an administrator Level.
      Best Regards,
      Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,401 Reputation points
    2021-07-07T17:49:14.03+00:00

    I don't have an exact solution but do have code in a GitHub repository that should help. Although the code is in a windows form project the following classes can be used in any project type.

    WindowsServices class has methods to

    • Stop a service
    • Start a service
    • Determine if a specific service is installed
    • Get service names
    • Get status of specific service

    Serviceinstaller class has methods to

    • Install a service
    • Uninstall a service
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.