C# 找出 Windows 服务的运行进程名称 .NET 1.1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/565658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding out Windows service's running process name .NET 1.1
提问by the berserker
We are using a badly written windows service, which will hang when we are trying to Stop it from code. So we need to find which process is related to that service and kill it. Any suggestions?
我们正在使用一个写得很糟糕的 Windows 服务,当我们试图从代码中停止它时,它会挂起。所以我们需要找到与那个服务相关的进程并杀死它。有什么建议?
采纳答案by Richard
WMI has this information: the Win32_Service class.
WMI 具有以下信息:Win32_Service 类。
A WQL query like
一个 WQL 查询,如
SELECT ProcessId FROM Win32_Service WHERE Name='MyServiceName'
using System.Management should do the trick.
使用 System.Management 应该可以解决问题。
From a quick look see: taskllist.exe /svc
and other tools from the command line.
快速查看:taskllist.exe /svc
和命令行中的其他工具。
回答by configurator
You can use
您可以使用
tasklist /svc /fi "SERVICES eq YourServiceName"
To find the process name and id, and also if the same process hosts other services.
查找进程名称和 id,以及同一进程是否托管其他服务。
回答by Daniel Richardson
You can use System.Management.MangementObjectSearcher
to get the process ID of a service and System.Diagnostics.Process
to get the corresponding Process
instance and kill it.
您可以使用System.Management.MangementObjectSearcher
获取服务的进程 ID 并System.Diagnostics.Process
获取相应的Process
实例并杀死它。
The KillService()
method in the following program shows how to do this:
KillService()
以下程序中的方法显示了如何执行此操作:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Management;
namespace KillProcessApp {
class Program {
static void Main(string[] args) {
KillService("YourServiceName");
}
static void KillService(string serviceName) {
string query = string.Format(
"SELECT ProcessId FROM Win32_Service WHERE Name='{0}'",
serviceName);
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(query);
foreach (ManagementObject obj in searcher.Get()) {
uint processId = (uint) obj["ProcessId"];
Process process = null;
try
{
process = Process.GetProcessById((int)processId);
}
catch (ArgumentException)
{
// Thrown if the process specified by processId
// is no longer running.
}
try
{
if (process != null)
{
process.Kill();
}
}
catch (Win32Exception)
{
// Thrown if process is already terminating,
// the process is a Win16 exe or the process
// could not be terminated.
}
catch (InvalidOperationException)
{
// Thrown if the process has already terminated.
}
}
}
}
}
回答by Daniel Richardson
Microsoft/SysInternals has a command-line tool called PsKill that allows you to kill a process by name. This tool also allows you to kill processes on other servers. Windows SysInternals
Microsoft/SysInternals 有一个名为 PsKill 的命令行工具,它允许您按名称杀死进程。此工具还允许您终止其他服务器上的进程。 Windows 系统内部
Usage: pskill [-t] [\computer [-u username [-p password]]] <process ID | name>
-t Kill the process and its descendants.
-u Specifies optional user name for login to remote computer.
-p Specifies optional password for user name. If you omit this you will be prompted to enter a hidden password.
用法:pskill [-t] [\computer [-u username [-p password]]] <进程ID | name>
-t 终止进程及其后代。
-u 指定用于登录远程计算机的可选用户名。
-p 指定用户名的可选密码。如果您省略此项,系统将提示您输入隐藏密码。
回答by Zhaph - Ben Duguid
I guess it's a two step process - if it's always the same service, you can easily find the process name using methods suggested in other answers.
我想这是一个两步过程 - 如果它总是相同的服务,您可以使用其他答案中建议的方法轻松找到进程名称。
I then have the following code in a class on a .NET 1.1 web server:
然后,我在 .NET 1.1 Web 服务器上的类中有以下代码:
Process[] runningProcs =
Process.GetProcessesByName("ProcessName");
foreach (Process runningProc in runningProcs)
{
// NOTE: Kill only works for local processes
runningProc.Kill();
}
The Kill methodcan throw a few exceptions that you should consider catching - especially the Win32Exception, that is thrown if the process cannot be killed.
该Kill方法可以抛出一些例外,你应该考虑醒目-尤其是Win32Exception,如果进程不能被杀死时抛出。
Note that the WaitForExit methodand HasExited propertyalso exist in the 1.1 world, but aren't mentioned on the documentation page for Kill in 1.1.
请注意,WaitForExit 方法和HasExited 属性也存在于 1.1 世界中,但未在 1.1 中的 Kill 文档页面中提及。
回答by the berserker
To answer exactly to my question - how to find Process related to some service:
准确回答我的问题 - 如何找到与某些服务相关的流程:
ManagementObjectSearcher searcher = new ManagementObjectSearcher
("SELECT * FROM Win32_Service WHERE DisplayName = '" + serviceName + "'");
foreach( ManagementObject result in searcher.Get() )
{
if (result["DisplayName"].ToString().ToLower().Equals(serviceName.ToLower()))
{
int iPID = Convert.ToInt32( result["ProcessId"] );
KillProcessByID(iPID, 1000); //some method that will kill Process for given PID and timeout. this should be trivial
}
}
}
}