做了个按钮,点击之后启动一个服务,我试过用servicecontroller类,和process类,甚至创建了一个bat文件启动,都是一样啊,在我本机上测试时好用,安到别人机器上之后就不好用了,到底是哪出问题了啊,各位高人帮忙~~服务是我自己写的,跟这有关系吗?

解决方案 »

  1.   

    ms-help://MS.MSDNQTR.2003FEB.2052/vbcon/html/vbwlkwalkthroughcreatingwindowsserviceapplication.htm
      

  2.   

    用这个,ServiceController ,哎...
      

  3.   

    参考这里的实现:使用C#控制远程计算机的服务
    http://blog.csdn.net/zhzuo/archive/2004/03/29/22025.aspx
      

  4.   

    try
                        {
                            ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, sc.MachineName, "AutoReport");
                            scp.Demand();
                            sc.Start();
                            
                            
                            
                            
                        //如果服务20秒后还没有启动成功,报告失败
                        
                            sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        }
                        catch (Exception err)
                        {
                            Label1.Text = "启动自动上报服务失败,请重新尝试";
                            Label1.Visible = true;
      

  5.   

    后来我又改成这样子try
                        {
                            ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, sc.MachineName, "AutoReport");
                            scp.Demand();
                            //sc.Start();
                            Process p = new Process();
                            p.StartInfo.FileName = "cmd.exe";                        p.StartInfo.UseShellExecute = false;                        p.StartInfo.RedirectStandardInput = true;                        p.StartInfo.RedirectStandardOutput = true;                        p.StartInfo.RedirectStandardError = true;                        // not display window
                            p.StartInfo.CreateNoWindow = true;                        p.Start();                        p.StandardInput.WriteLine("net start AutoReport ");                        p.StandardInput.WriteLine("exit");                        //string strRst = p.StandardOutput.ReadToEnd();
                            p.Close();
                        //如果服务20秒后还没有启动成功,报告失败
                        
                            sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        }
                        catch (Exception err)
                        {
                            Label1.Text = "启动自动上报服务失败,请重新尝试";
                            Label1.Visible = true;
      

  6.   

    再后来我又改成这样子,也都是到别人机器上就不好用啊,555try
                        {
                            ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, sc.MachineName, "AutoReport");
                            scp.Demand();
                            //sc.Start();
                            System.IO.FileStream fs =  System.IO.File.Create(Request.PhysicalApplicationPath + "report.bat");
                            byte[] bs = new byte[20];
                            string s = "net start AutoReport";
                            for (int i = 0; i < s.Length; i++)
                            {
                                bs[i] = (byte)s[i];
                            }
                            fs.Write(bs,0,bs.Length);
                            fs.Close();
                            
                            
                            Process p = new Process();
                            p.StartInfo.FileName = Request.PhysicalApplicationPath + "report.bat";
                            
                            p.StartInfo.UseShellExecute = false;
                            //Response.Write(p.StartInfo.UseShellExecute.ToString());                        p.StartInfo.RedirectStandardInput = true;                        p.StartInfo.RedirectStandardOutput = true;                        p.StartInfo.RedirectStandardError = true;                        // not display window
                            p.StartInfo.CreateNoWindow = true;                        p.Start();                        //p.StandardInput.WriteLine("net start AutoReport ");                        //p.StandardInput.WriteLine("exit");                        //string strRst = p.StandardOutput.ReadToEnd();
                            p.Close();
                        //如果服务20秒后还没有启动成功,报告失败
                        
                            sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                        }
                        catch (Exception err)
                        {
                            Label1.Text = "启动自动上报服务失败,请重新尝试";
                            Label1.Visible = true;
      

  7.   

    hdt(倦怠) 我机器只有MSDN Library
    Visual Studio 6.0 版呀
      

  8.   

    你在本机去调用远程的服务,你的代码应该实现不了,参考一下上面的链接。
    http://blog.csdn.net/zhzuo/archive/2004/03/29/22025.aspx
      

  9.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) ( ) 信誉:100 不好用就是点了按钮服务没起来呀
      

  10.   

    bestshl(快乐的Coder) ( ) 信誉:100 C#代码应该是在服务器上运行的吧?那我这里应该是启动的服务器上的服务吧?也就是说是属于启动本机服务的范围吧
      

  11.   

    不好用......
    windows service的用户密码名改了没,写在bat里面的。
      

  12.   

    servicecontroller 对象你是是怎样New出来的?
    让我们看看那行代码。
      

  13.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) ( ) 信誉:100 
    ServiceController sc = new ServiceController("AutoReport");
      

  14.   

    scow(怡红快绿之小橙子|和谐权是第4代人权) ( ) 
    你说的密码是指什么啊,我生成的bat文件里就一行 net start AutoReport这个服务程序我在制作的时候account给的是local system啊。
      

  15.   

    把“ ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, sc.MachineName, "AutoReport");
                            scp.Demand();

    这些注释掉试试!
      

  16.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) ( ) 
    我原来就是没有这些的,后来我研究了半天以为是权限的问题,就后加上的。结果如前啊
    如果我用手动启动的话,几秒的时间,和别的服务一样。但我发现我运行别人机器上时点了按钮就反应挺慢了,下边进度条走半天,不过好像也没超过这句话里设置的时间sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
      

  17.   

    对了,我们用的系统是win server 2003企业版,VS2005
      

  18.   

    你直接在CMD中输入net start AutoReport,看看会怎么样。
      

  19.   

    我是说在有问题的机器上输入net start AutoReport会怎么样??
    如果没问题的话,那我也不知道怎么回事了!
    如果有问题的话,那就是用户权限的问题了!
      

  20.   

    试过~好用~我也不知道怎么回事了~不过问题解决了~感谢大家帮忙~尤其是wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴)同学~
     解决办法:1将服务设置为自启动;
    2在用户手册上安装方法中标明“第一次安装后需要在服务管理器中手工启动服务”问题解决~
    再次感谢大家的帮助~~~~~~~~结帖~~~~~~~~