Skip to content

Commit

Permalink
获取dotnet后的文件名去掉后缀后作为进程名
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Aug 24, 2024
1 parent 2ac9a40 commit e15b2f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 49 deletions.
63 changes: 15 additions & 48 deletions NewLife.Core/Extension/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Linq;
using NewLife.Configuration;
using NewLife.Log;

Expand All @@ -14,61 +15,27 @@ namespace NewLife;
public static class ProcessHelper
{
#region 进程查找
/// <summary>获取进程名。dotnet/java进程取文件名,Windows系统中比较耗时</summary>
/// <summary>获取二级进程名。默认一级,如果是dotnet/java则取二级</summary>
/// <param name="process"></param>
/// <returns></returns>
public static String GetProcessName(this Process process)
{
var name = process.ProcessName;

//if (Runtime.Linux)
//{
// try
// {
// var file = $"/proc/{process.Id}/cmdline";
// if (File.Exists(file))
// {
// var lines = File.ReadAllText(file).Trim('\0', ' ').Split('\0');
// if (lines.Length > 1) name = Path.GetFileNameWithoutExtension(lines[1]);
// }
// }
// catch { }
//}
//else if (Runtime.Windows)
//{
// try
// {
// var dic = MachineInfo.ReadWmic("process where processId=" + process.Id, "commandline");
// if (dic.TryGetValue("commandline", out var str) && !str.IsNullOrEmpty())
// {
// var ss = str.Split(' ').Select(e => e.Trim('\"')).ToArray();
// str = ss.FirstOrDefault(e => e.EndsWithIgnoreCase(".dll", ".jar"));
// if (!str.IsNullOrEmpty()) name = Path.GetFileNameWithoutExtension(str);
// }
// }
// catch { }
//}

var args = GetCommandLineArgs(process.Id);
if (args != null && args.Length >= 2 && args[0].Contains("dotnet"))
var pname = process.ProcessName;
if (pname == "dotnet" || "*/dotnet".IsMatch(pname))
{
name = args[1];
var args = GetCommandLineArgs(process.Id);
if (args != null && args.Length >= 2 && args[0].Contains("dotnet"))
{
return Path.GetFileNameWithoutExtension(args[1]);
}
}

return name;
}

/// <summary>获取二级进程名。默认一级,如果是dotnet/java则取二级</summary>
/// <param name="process"></param>
/// <returns></returns>
public static String GetProcessName2(this Process process)
{
var pname = process.ProcessName;
if (
pname == "dotnet" || "*/dotnet".IsMatch(pname) ||
pname == "java" || "*/java".IsMatch(pname))
if (pname == "java" || "*/java".IsMatch(pname))
{
return GetProcessName(process);
var args = GetCommandLineArgs(process.Id);
if (args != null && args.Length >= 3 && args[0].Contains("java") && args[1] == "-jar")
{
return Path.GetFileNameWithoutExtension(args[2]);
}
}

return pname;
Expand Down
2 changes: 1 addition & 1 deletion Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private static void Test8()
foreach (var p in Process.GetProcessesByName("dotnet"))
{
//Runtime.FreeMemory(p.Id);
var name = p.GetProcessName2();
var name = p.GetProcessName();
XTrace.WriteLine("{0}\t{1}", p.Id, name);
}
}
Expand Down

0 comments on commit e15b2f0

Please sign in to comment.