概要:
- 查询远程机器上的注册表
- 捕获输出以在应用程序中使用
- 需要使用C#
- 到目前为止,所有使用的方法都只能在本地机器上查询
- 非常感谢任何帮助
完整问题:
我需要找到一种在C#中运行命令行命令并捕获其输出的方法。我知道如何在Perl中做到这一点,在下面是我在Perl中使用的代码。
#machine to check
my $pc = $_[0];
#create location of registry query
my $machine = "\\\\".$pc."\\HKEY_USERS";
#run registry query
my @regQuery= `REG QUERY $machine`;
欢迎提供在C#中实现此操作的任何建议。到目前为止,我已经尝试使用RegistryKey OurKey = Registry.Users方法,它工作得很好,但我无法查询远程计算机上的注册表。
请告诉我如果您需要更多信息。
解决方案:(感谢@Robaticus)
private void reg(string host)
{
string build = "QUERY \\\\" + host + "\\HKEY_USERS";
string parms = @build;
string output = "";
string error = string.Empty;
ProcessStartInfo psi = new ProcessStartInfo("reg.exe", parms);
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
System.Diagnostics.Process reg;
reg = System.Diagnostics.Process.Start(psi);
using (System.IO.StreamReader myOutput = reg.StandardOutput)
{
output = myOutput.ReadToEnd();
}
using (System.IO.StreamReader myError = reg.StandardError)
{
error = myError.ReadToEnd();
}
Output.AppendText(output + "\n");
}
RegistryKey.OpenRemoteBaseKey
吗?http://msdn.microsoft.com/zh-cn/library/8zha3xws.aspx - Tim RobinsonRegistryKey
对象,你可以在上面调用GetSubKeyNames
。 - Tim Robinson