(接上一个帖子“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.   

    原贴在这里:http://community.csdn.net/Expert/topic/4879/4879115.xml?temp=.9829218
      

  2.   

    如果是wmi的话,你可以附加上connectionoptions,这方面的参数你可以用当前系统管理员的用户名和密码。然后看看是否可以startservice。
      

  3.   

    就是前面我说得那个控制连接服务器的,开启、关闭服务的类里面好像写了connectionoptions的相关参数,但是,只要加上用户名密码,就连接不上,不加,直接用"127.0.0.1"或“localhost”就都可以连接上。纳闷阿。要不我把源码发给你看看,你看看有什么问题好么?============================================================public Win32ServiceManager(string host,string userName,string password)
    {
    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;
    }
    }