Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取.NET中远程计算机上环境变量的实际值_C#_.net_Windows_Wmi_Environment Variables - Fatal编程技术网

C# 获取.NET中远程计算机上环境变量的实际值

C# 获取.NET中远程计算机上环境变量的实际值,c#,.net,windows,wmi,environment-variables,C#,.net,Windows,Wmi,Environment Variables,我正在尝试获取环境变量的实际值,这是我到目前为止得到的: string query = string.Format("Select VariableValue From Win32_Environment Where Name = '{0}'", variableName); using (var searcher = new ManagementObjectSearcher(query)) using (ManagementObject result = searcher.Get().Cas

我正在尝试获取环境变量的实际值
这是我到目前为止得到的:

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"]);
}
string query=string.Format(“从Win32_环境中选择VariableValue,其中Name='{0}',variableName);
使用(var searcher=newmanagementobjectsearcher(查询))
使用(managementobjectresult=searcher.Get().Cast().FirstOrDefault())
{
如果(结果!=null)
返回Convert.ToString(结果[“VariableValue”]);
}
这是可行的,但问题是:将'windir'作为名称传递会将'%SystemRoot%'作为值传递。我真正想要的是实际路径,即“C:\Windows”

尝试使用递归获取'SystemRoot'的,但未找到匹配项


如何确保返回实际值

使用
Environment.GetFolderPath(Environment.SpecialFolder.System)
您不能使用Win32\u环境进行此操作,但可以使用远程注册表

RegistryKey environmentKey = RegistryKey.OpenRemoteBaseKey(
      RegistryHive.LocalMachine, "\\server");
RegistryKey key = environmentKey.OpenSubKey(
      @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", false);
string value = (string)key.GetValue("System");
对于(比如
%SystemRoot%
)没有方便的方法

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

  • %SystemRoot%:HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRoot或

    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRoot 从Win32\u操作系统中选择Windows目录
  • %SystemDrive%可以通过检查%SystemRoot%来确定


%AppData%这样的变量依赖于用户,可以在HKEY\U USERS\\Software\Microsoft\Windows\CurrentVersion\Explorer\user Shell Folders\AppData下找到。我知道这充其量是有创意的,但这似乎是最简单的解决方案:
开销可能太大了

HKEY_USERS\<user SID>\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\AppData
        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();
        }

为什么%SystemRoot%不可接受?如果你把它放在资源管理器窗口中,你会转到windows的安装目录。因为%SystemRoot%理论上可能是D:\Windows\的。可能与@Ramhound重复:我正在编写一个方法,使用Win32\u Process.Create在远程计算机上运行命令。环境变量似乎没有被识别,所以我想自己替换它们。如果有办法让他们自动识别,这也会解决我的问题。问题标题是在远程计算机上。这会让我得到%SystemRoot%,但不会得到%ProgramFiles%或其他。我想我正在寻找环境的远程版本。GetEnvironmentVariable@David:没有远程版本。另一种方法是远程执行
环境.GetEnvironmentVariable
,例如,通过在远程机器上启动程序。
开销可能太大?
一点也不。特别是当设置了追踪兔子踪迹试图解析递归变量的开销时:
%ComSpec%=%SystemRoot%\System32\cmd.exe
。真是一场噩梦。我正要放弃,突然发现了你的答案。好把戏。
        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();
        }