c# - Win32_ProcessStartTrace 查询中的截断 ProcessName

标签 c# .net process

我正在使用此代码来监视进程:

var startWatch = new ManagementEventWatcher(
    "SELECT * FROM Win32_ProcessStartTrace");
startWatch.EventArrived += startWatch_EventArrived;
startWatch.Start();

var stopWatch = new ManagementEventWatcher(
    "SELECT * FROM Win32_ProcessStopTrace");
stopWatch.EventArrived += stopWatch_EventArrived;
stopWatch.Start();

问题是 - ProcessName 属性在两个回调中都被截断为 14 个字符。
var name = e.NewEvent.Properties["ProcessName"].Value.ToString();

两个进程(监视和监视)都是 x64 .NET 控制台应用程序。

任何人都知道可能是什么原因?

最佳答案

使用 __InstanceCreationEvent/__InstanceDeletionEvent反而

例子

var startWatch = new ManagementEventWatcher(
    "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'");
startWatch.EventArrived += startWatch_EventArrived;
startWatch.Start();

var stopWatch = new ManagementEventWatcher(
    "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'");
stopWatch.EventArrived += stopWatch_EventArrived;
stopWatch.Start();

事件示例
// e.NewEvent now have only 3 properties, we should focus on TargetInstance property
var targetInstance = (ManagementBaseObject) e.NewEvent["TargetInstance"];
// TargetInstance has more than 40 properties, some properties can be null
var name = targetInstance["Name"]?.ToString();

在 .NET Core 3.1 上测试 System.Management NuGet 包。


// Win32_ProcessStartTrace
"League of Legends.exe"
// Win32_ProcessStopTrace
"League of Le" // How can this happen??? Like how???


// __InstanceCreationEvent
"League of Legends.exe"
// __InstanceDeletionEvent
"League of Legends.exe"

关于c# - Win32_ProcessStartTrace 查询中的截断 ProcessName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38693623/

相关文章:

c# - 为什么 null 不是 Nullable<> 类型的实例?

python - 使用 Python 在 Mac 上获取开放应用程序

c - 如何区分子进程?

c# - 如何在C Sharp中使用DLL

c# - AvalonEdit:确定您是否在评论中

c# - ViewModel 在操作方法中获取空值

.net - .Net等效于x86 ASM命令XADD

c# - 关于.Net继承的问题

c# - 使用枚举值

linux - 如何修复 java.lang.OutOfMemoryError : unable to create new native thread