问题描述
我想从进程A中的C#代码中读取进程B的环境变量.我已经在C ++中看到了一些解决方案,但是还没有尝试将其适应C#.是否可以从C#中获得,如果没有,是否有人包装了C ++解决方案?
I want to read the environment variables of process B from C# code in process A. I have seen some solutions for this in C++ but haven't tried adapting these to C#. Is this possible from C#, and if not, has anyone wrapped a C++ solution yet?
推荐答案
我浏览了Isalamon和Daniel Hilgarth提供的链接,以及CLR Profiler的 GetServicesEnvironment()
方法中的代码,似乎正在做同样的事情,经过一些测试,发现最可靠的解决方案是 Oleksiy的代码(带有P/Invoke的纯C#代码),他在此博客文章.它仍然有局限性,您必须是64位进程才能读取另一个64位进程的环境变量.
I've skimmed through the links provided by Isalamon and Daniel Hilgarth, and also the code in CLR Profiler's GetServicesEnvironment()
method which seems to be doing the same thing, and after a bit of testing found that the most reliable solution is Oleksiy's code (pure C# with P/Invoke) which he published in this blog post. It still has the limitation, where you have to be a 64bit process to read the env vars of another 64bit process.
这篇关于从C#进程中读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!