首页 新闻 搜索 专区 学院

如何用一个Windows服务去停止另一个Windows服务?

0
悬赏园豆:10 [已关闭问题] 关闭于 2012-08-16 23:23

在有些机器上可以,但是在有些机器上没反应,服务仍然牌运行状态

RunCmd(@"C:\CdmSecurityClient\ClientService\net.exe", "stop CdmSecurityClient /y");


        private void RunCmd(string fileName, string arguments)
        {
            Process process = new Process();
            process.StartInfo = new ProcessStartInfo();
            process.StartInfo.FileName = fileName;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            process.WaitForExit(60 * 1000);
            process.Close();
        }

有可能是哪方面的原因呢?程序没有抛异常。

问题补充:

I think the problem is why serviceController.Stop() throw TimeoutException?

The services both are run in Local System, It's about account right?

==================

I think I found the key point, I should grant permission for LocalSystem to have right to stop that service using "sc sdset ......", but the command is very complex, who know how to use it?

灰灰狼的主页 灰灰狼 | 初学一级 | 园豆:5
提问于:2012-08-10 21:47
< >
分享
所有回答(3)
0

http://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicecontroller_members(v=vs.80)

 

ServiceController

可以得到你要的服务,然后stop方法。

chenping2008 | 园豆:9836 (大侠五级) | 2012-08-10 22:07

哇,新思路,我会试试的

支持(0) 反对(0) 灰灰狼 | 园豆:5 (初学一级) | 2012-08-11 14:05

@灰灰狼: 

  ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController service in services)
            {
                Console.WriteLine("The {0} service is currently {1}.",service.DisplayName,service.Status);
            }

可以到的服务 ,然后有个Stop 方法。

支持(0) 反对(0) chenping2008 | 园豆:9836 (大侠五级) | 2012-08-12 16:14

@chenping2008: I tried it out, the method Stop throw an exception: TimeoutException

:(

支持(0) 反对(0) 灰灰狼 | 园豆:5 (初学一级) | 2012-08-13 12:52

@灰灰狼: 

foreach (ServiceController service in services)
            {
                if (service.ServiceName.Equals("memcached"))
                {
                    if (service.CanStop)
                    {
                        service.Stop();
                        break;
                    }
                }
            }

I use this code stop memcached service, the test result is ok. no exception throw.

the stop method only can throw two exceptions:

1.   System.ComponentModel.Win32Exception
2.   System.InvalidOperationException

支持(0) 反对(0) chenping2008 | 园豆:9836 (大侠五级) | 2012-08-13 16:00
0
RunCmd(@"C:\windows\system32\sc.exe", "stop CdmSecurityClient");
I,Robot | 园豆:9563 (大侠五级) | 2012-08-11 08:41

哇,还有这个命令啊,还以为只有net.exe呢,会试试的

支持(0) 反对(0) 灰灰狼 | 园豆:5 (初学一级) | 2012-08-11 14:06

@灰灰狼: Still don't response sc.exe just like net.exe...

支持(0) 反对(0) 灰灰狼 | 园豆:5 (初学一级) | 2012-08-13 13:11
0

应该是权限问题,但没找到如何解决,采用了另外一种变态方法,没有推广的价值,就不说了。感谢大家帮忙。

灰灰狼 | 园豆:5 (初学一级) | 2012-08-14 07:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册