windows 在.NET中获取远程机器上环境变量的真实值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7230699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:57:16  来源:igfitidea点击:

Get the real values of environment variables on a remote machine in .NET

c#.netwindowswmienvironment-variables

提问by David

I'm trying to get the actual valuesof environment variables.
This is what I have so far:

我试图让实际值环境变量
这是我到目前为止:

string query = string.Format("Select VariableValue From Win32_Environment Where Name = '{0}'", variableName);

using (var searcher = new ManagementObjectSearcher(query))
using (ManagementObject result = searcher.Get().Cast<ManagementObject>().FirstOrDefault())
{
    if (result != null)
    return Convert.ToString(result["VariableValue"]);
}

That works, but here's the problem: passing 'windir' as namegets '%SystemRoot%' as value. What I really wantis the actual path, i.e. 'C:\Windows'.

这是有效的,但问题是:将'windir' 作为 name传递给'%SystemRoot%' 作为 value。我真正想要的实际路径,即“C:\Windows”。

I triedusing recursionto get the value of 'SystemRoot'but no matcheswere found.

尝试使用递归来获取“SystemRoot”值,没有找到匹配项。

How can I make sure that the real valuesget returned?
Thx!

如何确保返回真实值
谢谢!

回答by VVS

For system path variables(like %SystemRoot%) there's no convenient way.

对于系统路径变量(如%SystemRoot%),没有方便的方法。

You have to look for these values yourself by reading the corresponding registry values. Heres' a (not complete) list of some of these system variables:

您必须自己通过阅读相应的注册表值来查找这些值。这是其中一些系统变量的(不完整)列表:

  • %SystemRoot%:

    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRoot
    or

    select windowsdirectory from Win32_OperatingSystem
  • %SystemDrive%can be determined by examining %SystemRoot%

  • %SystemRoot%:

    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRoot
    或者

    select windowsdirectory from Win32_OperatingSystem
  • %SystemDrive%可以通过检查 %SystemRoot% 来确定

Variables like %AppData%are user dependent and found under

%AppData%这样的变量依赖于用户,可以在下面找到

HKEY_USERS\<user SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\AppData

回答by David

I know it's creative at best but this seems to be the simplest solution:
Too much overhead perhaps?

我知道它充其量是有创意的,但这似乎是最简单的解决方案:
也许开销太大?

        using (var process = new Process())
        {
            process.StartInfo.FileName = @"C:\PsTools\PsExec.exe";
            process.StartInfo.Arguments = @"\machineName cmd /c echo " + environmentVar;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            return process.StandardOutput.ReadToEnd();
        }

回答by TheCodeKing

You can't use Win32_Environment for this, but you can use remote registry.

您不能为此使用 Win32_Environment,但您可以使用远程注册表。

RegistryKey environmentKey = RegistryKey.OpenRemoteBaseKey(
      RegistryHive.LocalMachine, "\server");
RegistryKey key = environmentKey.OpenSubKey(
      @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", false);
string value = (string)key.GetValue("System");

回答by Schu

use Environment.GetFolderPath(Environment.SpecialFolder.System)

Environment.GetFolderPath(Environment.SpecialFolder.System)