我想知道这是否可以从C#中的werfault进程获取相关的进程ID。我不想禁用werfault服务,只获取相关(冻结)进程ID。我写了这段代码:
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName.ToLower().Contains("werfault"))
{
//getting related process id?
}
}
例如:werfault服务报告' programX已停止工作'。我使用上面的代码找到了werfault进程,然后将其删除并检索programX pid(我现在无法做到)。
我在这里找到了部分答案:How to launch crashing (rarely) application in subprocess但这适用于python。
这可以检索吗?我需要任何外部库吗?
答案 0 :(得分:0)
wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chown -R vagrant:vagrant ~/.ssh
答案 1 :(得分:0)
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName.ToLower().Contains("werfault"))
{
// Get the CommandLine string from the werfault.exe
string startupParam = GetCommandLine(p);
// Get the ProcessID of the frozen Process.
// Sure you can optimize this part, but it works in this case :)
int pID = int.Parse(startupParam.Split(new string[] { "-p" }, StringSplitOptions.None).
Last().Split(new string[] { "-s" }, StringSplitOptions.None).First().Trim());
// Get the frozen Process.
Process frozenProcess = Process.GetProcessById(pID);
}
}
/// <summary>
/// Returns the CommandLine from a Process.
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
private static string GetCommandLine(Process pProcess)
{
// Create a new CommandLine with the FileName of the given Process.
var commandLine = new StringBuilder(pProcess.MainModule.FileName);
commandLine.Append(" ");
// Now we need to query the CommandLine of the process with ManagementObjectSearcher.
using (var searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + pProcess.Id))
{
// Append the arguments to the CommandLine.
foreach (var @object in searcher.Get())
{
commandLine.Append(@object["CommandLine"]);
commandLine.Append(" ");
}
}
// Return the CommandLine.
return commandLine.ToString();
}