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
Get the real values of environment variables on a remote machine in .NET
提问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
orselect 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)