(接上一个帖子“asp.net能否操作windows服务?”继续问)
不好意思阿,又要打扰你们了。我今天试了一天,还是搞不定,是这样的情况:
1、服务器上已经安装了一服务,名称是“NetSafe”;
2、asp.net页面上某一个按钮,用来开启这个服务,下面有代码:
string SHost="127.0.0.1";
//string SName="administrator"; //这里如果用管理员用户名反而连不上
string SName="";
string SPass="";
//string SPass="123456";
this.serviceManager = new Win32ServiceManager(SHost,SName,SPass);
string startRst = this.serviceManager.StartService("Themes"); //里面是服务的名称,不是显示的名称
if(startRst==null)
Label11.Text ="success!"; //如果成功显示这个信息。结果显示信息,但并没有开启服务成功
else
Label11.Text ="error!";3、另外还有一个类,就是专门的用来连接服务,以及操作服务的相关代码;这部分应该没有问题(下面是开启服务的方法)
// 开启指定的服务
public string StartService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
try
{
if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]
mo.InvokeMethod("StartService",null);
}
catch(ManagementException e)
{
strRst =e.Message;
}
return strRst;
}4、而且以上代码,我用windows应用程序条是完全正常。但是一到asp.net下面,就出现问题,可以连接正常,也能显示相关服务的信息。但就是不能打开和停止服务,也不报错。救命啊,实在受不了阿!!!!!!!!!!
不好意思阿,又要打扰你们了。我今天试了一天,还是搞不定,是这样的情况:
1、服务器上已经安装了一服务,名称是“NetSafe”;
2、asp.net页面上某一个按钮,用来开启这个服务,下面有代码:
string SHost="127.0.0.1";
//string SName="administrator"; //这里如果用管理员用户名反而连不上
string SName="";
string SPass="";
//string SPass="123456";
this.serviceManager = new Win32ServiceManager(SHost,SName,SPass);
string startRst = this.serviceManager.StartService("Themes"); //里面是服务的名称,不是显示的名称
if(startRst==null)
Label11.Text ="success!"; //如果成功显示这个信息。结果显示信息,但并没有开启服务成功
else
Label11.Text ="error!";3、另外还有一个类,就是专门的用来连接服务,以及操作服务的相关代码;这部分应该没有问题(下面是开启服务的方法)
// 开启指定的服务
public string StartService(string serviceName)
{
string strRst = null;
ManagementObject mo = this.managementClass.CreateInstance();
mo.Path = new ManagementPath(this.strPath+".Name=\""+serviceName+"\"");
try
{
if((string)mo["State"]=="Stopped")//!(bool)mo["AcceptStop"]
mo.InvokeMethod("StartService",null);
}
catch(ManagementException e)
{
strRst =e.Message;
}
return strRst;
}4、而且以上代码,我用windows应用程序条是完全正常。但是一到asp.net下面,就出现问题,可以连接正常,也能显示相关服务的信息。但就是不能打开和停止服务,也不报错。救命啊,实在受不了阿!!!!!!!!!!
{
this.strPath = "\\\\"+host+"\\root\\cimv2:Win32_Service";
this.managementClass = new ManagementClass(strPath);
if(userName!=null&&userName.Length>0)
{
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Username = userName;
connectionOptions.Password = password;
ManagementScope managementScope = new ManagementScope( "\\\\" +host+ "\\root\\cimv2",connectionOptions) ;
this.managementClass.Scope = managementScope;
}
}