简体   繁体   中英

run windows service from asp.net web page

我有一个用vb编写的网页,我需要启动将安装在服务器中的Windows服务。

haven't tested.

pleas try if works or not. you could add following code in btn click event.

dim controller as new ServiceController

controller.MachineName = "." //try the machine name
controller.ServiceName = "service name"
dim status as string = controller.Status.ToString

' Stop the service
controller.Stop()

' Start the service
controller.Start()

Depending on the permissions that the web site account has, you can start / stop services.

In addition to what others have answered, you can shell out NET START with the appropriate parameters.

You can also do this for remote computers as long as the permissions are granted (I think it has to be a domain account for this).

You first need to add a reference to the System.ServiceProcess assembly. The following code gives you roughly what you want to do (I am using a Label control called messageLabel in the following):

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class StartService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string serviceName = "Remote Registry";
        try
        {
            StartServiceByName(serviceName);
        }
        catch (Exception ex)
        {
            messageLabel.Text = ex.ToString().Replace("\r\n", "<BR>");
            return;
        }
        messageLabel.Text = String.Format("Service {0} started.", serviceName);
    }

    private void StartServiceByName(string serviceName)
    {
        ServiceController serviceController = new ServiceController(serviceName);
        serviceController.Start();
    }
}

However, there is a further thing - you need to have the web server have permission to change this service - which is something that normally can be only done with Administration rights.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM