如何使用c#启动未通过asp

如何使用c#启动未通过asp

本文介绍了如何使用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

userName="Domain/Username" password="password" />


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);
}
}


}

推荐答案



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

08-18 10:43