我需要帮助来使用 C# 终止 Windows 服务,现在要终止该服务,请使用以下选项:
从命令:
sc queryex ServiceName
发现后PID
服务的
taskkill /pid 1234(exemple) /f
为了便于阅读,但如果您明白我的意思,我将使用单独的方法 IsServiceInstalled、IsServiceRunning、StopService 等将服务交互分离到它自己的类中。
另外,通过停止服务,它应该已经终止了该进程,但我也包括了您将如何做类似的事情。如果服务不会停止,并且您通过终止进程来停止它,我会查看服务代码(如果您有权访问它,但它的构建不正确)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceController sc = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName.Equals("MyServiceNameHere"));
if (sc != null)
{
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
Process[] procs = Process.GetProcessesByName("MyProcessName");
if (procs.Length > 0)
{
foreach (Process proc in procs)
{
//do other stuff if you need to find out if this is the correct proc instance if you have more than one
proc.Kill();
}
}
}
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)