且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#获取其他进程的环境变量

更新时间:2021-10-16 10:12:31

该文档没有充分记录,但是

It is not well documented, but ProcessStartInfo.EnvironmentVariables does not return the environment of a specific process.

如果您查看参考源,您会发现 调用GetEnvironmentStrings 函数.该函数返回一个指向内存块的指针,该内存块包含调用进程的环境变量 (系统变量和用户环境变量)".

If you look at the reference source, you will see that under the hood the GetEnvironmentStrings function is called. This function returns "a pointer to a block of memory that contains the environment variables of the calling process (both the system and the user environment variables)".

访问另一个进程的环境块比较棘手.Oleksiy Gapotchenko已撰写了优秀博客文章,并提供了示例解决方案.

Accessing the environment block of another process is a bit more tricky. Oleksiy Gapotchenko has written an excellent blog post and also provides a sample solution.