如何使用c#启动未通过asp.net安装在计算机上的Windows服务? [英] How to start a windows service not installed on computer via asp.net with c#?

查看:61
本文介绍了如何使用c#启动未通过asp.net安装在计算机上的Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过带有c#的asp.net启动未安装在计算机上的Windows服务?我可以启动系统中安装的服务。

我的代码如下:

在web.config下

How to start a windows service not installed on computer via asp.net with c#?I am able to start the service installed in system.
My code as follows:
Under web.config

<identity impersonate="true">
     userName="Domain/Username" password="password" /></identity>





在aspx.cs页面下



Under aspx.cs page

private void StartService(string serviceName, int timeoutMilliseconds)
    {
        Configuration objConfigFile;
        //getting an instance of the configuration file
        objConfigFile = WebConfigurationManager.OpenWebConfiguration(
                               HttpContext.Current.Request.ApplicationPath);

        //getting an instance of the "identity" section of the cofiguration file
        IdentitySection objIdentitySection =
          (IdentitySection)objConfigFile.GetSection("system.web/identity");

        if (objIdentitySection != null)
        {
            string username = objIdentitySection.UserName;
            string password = objIdentitySection.Password;
            bool impersonate = objIdentitySection.Impersonate;
            //Configuration currentConfiguration = objIdentitySection.C.CurrentConfiguration;

            //Obviously you won't be doing this. The lines below are just for testing purpose
         

            ServiceController service = new ServiceController(serviceName);
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch(Exception ex)
            {
                throw (ex);
            }
        }


    }

推荐答案

参见如何启动未通过asp安装在计算机上的Windows服务.net with c#?I [ ^ ]。


这篇关于如何使用c#启动未通过asp.net安装在计算机上的Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆