如何捕获在C#Shell命令的输出? [英] How to capture Shell command output in C#?
问题描述
摘要:
- 在远程机器上查询注册表
- 捕获输出到应用程序中使用
- 需要在CSHARP
- 在迄今为止的所有方法中使用,只能查询本地计算机上
- 在任何的希望是很大的AP preciated
完整的问题:
我需要找到一种方式来运行在C#一个命令行命令,并捕获它的输出。我知道如何做到这一点在Perl,下方则是code我会在Perl使用。
#machine检查
我的$ PC = $ _ [0];
注册表查询#create位置
。我的$机=\\\\$ PC\\ HKEY_USERS。
#run注册表查询
我的@ regQuery =`REG QUERY $ machine`;
这是如何做到这一点的csharp的任何建议将受到欢迎。到目前为止,香港专业教育学院尝试使用的RegistryKey OurKey = Registry.Users方法,它的伟大工程,但我不能查询远程计算机上的注册表。
请让我知道如果你需要任何更多的信息。
解决方案:(谢谢@Robaticus)
私人无效寄存器(字符串主机)
{
字符串构建=QUERY \\\\+主机+\\ HKEY_USERS;
字符串PARMS = @build;
字符串输出=;
字符串错误=的String.Empty;
的ProcessStartInfo磅=新的ProcessStartInfo(REG.EXE,PARMS);
psi.RedirectStandardOutput = TRUE;
psi.RedirectStandardError = TRUE;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
psi.UseShellExecute = FALSE;
的System.Diagnostics.Process章;
章= System.Diagnostics.Process.Start(psi)的;
使用(就是System.IO.StreamReader myOutput = reg.StandardOutput)
{
输出= myOutput.ReadToEnd();
}
使用(就是System.IO.StreamReader myError = reg.StandardError)
{
错误= myError.ReadToEnd();
}
Output.AppendText(输出+\ N);
}
您可能需要调整这个有点,但这里的一些code重定向输出和错误的过程(略从原来的修改):
字符串PARMS = @QUERY \\计算机\ HKEY_USERS;
字符串输出=;
字符串错误=的String.Empty;
的ProcessStartInfo磅=新的ProcessStartInfo(REG.EXE,PARMS);
psi.RedirectStandardOutput = TRUE;
psi.RedirectStandardError = TRUE;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
psi.UseShellExecute = FALSE;
的System.Diagnostics.Process章;
章= System.Diagnostics.Process.Start(psi)的;
使用(就是System.IO.StreamReader myOutput = reg.StandardOutput)
{
输出= myOutput.ReadToEnd();
}
使用(就是System.IO.StreamReader myError = reg.StandardError)
{
错误= myError.ReadToEnd();
}
Summary:
- query registry on remote machine
- capture output to use in application
- needs to be in csharp
- so far all methods used can only query on the local machine
- any hope is greatly appreciated
Full issue:
I need to find a way to run a commandline command in csharp and capture its output. I know how to do this in Perl, below is the code I would use in 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`;
Any suggestions on how to do this in csharp would be welcome. So far ive tried using the RegistryKey OurKey = Registry.Users method and it works great but i can not query the registry on a remote machine.
Please let me know if you need any more information.
SOLUTION:(Thank you to @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");
}
You might have to tweak this a bit, but here's some (slightly modified from the original) code that redirects stdout and stderr for a process:
string parms = @"QUERY \\machine\HKEY_USERS";
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();
}
这篇关于如何捕获在C#Shell命令的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!