获取不同进程的环境变量

Get environment variables of a different process

我正在使用 Service Fabric,我想创建一个所选进程的环境变量观察器。

我想到了这个方法:

var processes = Process.GetProcessesByName(processName);
foreach (var process in processes){
    var environmentVariables = process.StartInfo.EnvironmentVariables;
    foreach (DictionaryEntry envVar in environmentVariables){
        // envVar.Key, enVar.Value ...
    }
}

但是我不能使用 StartInfo,因为这里没有启动进程。 是否有任何其他方法来检索进程的环境变量 (key/value)(由 GetProcessByName/ID 检索)?

错误:

System.InvalidOperationException: Process was not started by this object, so requested information cannot be determined.

在发布的链接中,我找到了 Oleksiy Gapotchenko 的博客。他开发了一个随时可用的 nuget 包。

Reads environment variables of a process. The functionality is achieved by reading the process environment block (PEB) at the operating system level.

blog.gapotchenko/reading-environment-variables

github/Gapotchenko.FX.Diagnostics.Process

nuget/packages/Gapotchenko.FX.Diagnostics.Process