在有些机器上可以,但是在有些机器上没反应,服务仍然牌运行状态
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?
http://msdn.microsoft.com/zh-cn/library/system.serviceprocess.servicecontroller_members(v=vs.80)
ServiceController
可以得到你要的服务,然后stop方法。
哇,新思路,我会试试的
@灰灰狼:
ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { Console.WriteLine("The {0} service is currently {1}.",service.DisplayName,service.Status); }
可以到的服务 ,然后有个Stop 方法。
@chenping2008: I tried it out, the method Stop throw an exception: TimeoutException
:(
@灰灰狼:
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
RunCmd(@"C:\windows\system32\sc.exe", "stop CdmSecurityClient");
哇,还有这个命令啊,还以为只有net.exe呢,会试试的
@灰灰狼: Still don't response sc.exe just like net.exe...
应该是权限问题,但没找到如何解决,采用了另外一种变态方法,没有推广的价值,就不说了。感谢大家帮忙。