概要:
完全な問題:
Csharpでコマンドラインコマンドを実行してその出力をキャプチャする方法を見つける必要があります。私はこれを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`;
Csharpでこれを行う方法についての提案は大歓迎です。これまでのところ、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");
}
これを少し微調整する必要があるかもしれませんが、プロセスのstdoutとstderrをリダイレクトするいくつかの(元のコードからわずかに変更された)コードを次に示します。
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#プログラムで実行できます。これを行うにはいくつかの方法があります。1つは、 blog に示すように非同期プロセスコマンドを使用する方法です。コマンドラインに対してアクティブな方法で書き込みと読み取りを行うだけです。ここから、何を達成したいのか、コマンドラインでそれを行う方法を理解するだけです。次に、それをプログラムに接続します
class Program
{
static void Main(string[] args)
{
LaunchCommandAsProcess cmd = new LaunchCommandAsProcess();
cmd.OutputReceived += new LaunchCommandAsProcess.OutputEventHandler(launch_OutputReceived);
cmd.SendCommand("help");
cmd.SendCommand("ipconfig");
cmd.SyncClose();
}
/// Outputs normal and error output from the command Prompt.
static void launch_OutputReceived(object sendingProcess, EventArgsForCommand e)
{
Console.WriteLine(e.OutputData);
}
}
ご覧のとおり、クラスをインスタンス化し、出力イベントを処理し、コマンドプロンプトに入力したときと同じようにコマンドの記述を開始するだけです。
仕組みは次のとおりです。
public class LaunchCommandAsProcess
{
public delegate void OutputEventHandler(object sendingProcess, EventArgsForCommand e);
public event OutputEventHandler OutputReceived;
private StreamWriter stdIn;
private Process p;
public void SendCommand(string command)
{
stdIn.WriteLine(command);
}
public LaunchCommandAsProcess()
{
p = new Process();
p.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
stdIn = p.StandardInput;
p.OutputDataReceived += Process_OutputDataReceived;
p.ErrorDataReceived += Process_OutputDataReceived;
p.BeginOutputReadLine();
p.BeginErrorReadLine();
}
///
/// Raises events when output data has been received. Includes normal and error output.
///
/// /// private void Process_OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine)
{
if (outLine.Data == null)
return;
else
{
if (OutputReceived != null)
{
EventArgsForCommand e = new EventArgsForCommand();
e.OutputData = outLine.Data;
OutputReceived(this, e);
}
}
}
///
/// Synchronously closes the command promp.
///
public void SyncClose()
{
stdIn.WriteLine("exit");
p.WaitForExit();
p.Close();
}
///
/// Asynchronously closees the command Prompt.
///
public void AsyncClose()
{
stdIn.WriteLine("exit");
p.Close();
}
}
public class EventArgsForCommand : EventArgs
{
public string OutputData { get; internal set; }
}
これが私が使っているクラスです。 ブログ投稿 少し前に見つけたコードを元にしていますが、他にもさまざまな変更が加えられています。
using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
namespace SonomaTechnologyInc {
/// <summary>
/// Utility class for working with command-line programs.
/// </summary>
public class Subprocess {
private Subprocess() { }
/// <summary>
/// Executes a command-line program, specifying a maximum time to wait
/// for it to complete.
/// </summary>
/// <param name="command">
/// The path to the program executable.
/// </param>
/// <param name="args">
/// The command-line arguments for the program.
/// </param>
/// <param name="timeout">
/// The maximum time to wait for the subprocess to complete, in milliseconds.
/// </param>
/// <returns>
/// A <see cref="SubprocessResult"/> containing the results of
/// running the program.
/// </returns>
public static SubprocessResult RunProgram(string command, string args, int timeout) {
bool timedOut = false;
ProcessStartInfo pinfo = new ProcessStartInfo(command);
pinfo.Arguments = args;
pinfo.UseShellExecute = false;
pinfo.CreateNoWindow = true;
//pinfo.WorkingDirectory = ?
pinfo.RedirectStandardOutput = true;
pinfo.RedirectStandardError = true;
Process subprocess = Process.Start(pinfo);
ProcessStream processStream = new ProcessStream();
try {
processStream.Read(subprocess);
subprocess.WaitForExit(timeout);
processStream.Stop();
if(!subprocess.HasExited) {
// OK, we waited until the timeout but it still didn't exit; just kill the process now
timedOut = true;
try {
subprocess.Kill();
processStream.Stop();
} catch { }
subprocess.WaitForExit();
}
} catch(Exception ex) {
subprocess.Kill();
processStream.Stop();
throw ex;
} finally {
processStream.Stop();
}
TimeSpan duration = subprocess.ExitTime - subprocess.StartTime;
float executionTime = (float) duration.TotalSeconds;
SubprocessResult result = new SubprocessResult(
executionTime,
processStream.StandardOutput.Trim(),
processStream.StandardError.Trim(),
subprocess.ExitCode,
timedOut);
return result;
}
}
/// <summary>
/// Represents the result of executing a command-line program.
/// </summary>
public class SubprocessResult {
readonly float executionTime;
readonly string stdout;
readonly string stderr;
readonly int exitCode;
readonly bool timedOut;
internal SubprocessResult(float executionTime, string stdout, string stderr, int exitCode, bool timedOut) {
this.executionTime = executionTime;
this.stdout = stdout;
this.stderr = stderr;
this.exitCode = exitCode;
this.timedOut = timedOut;
}
/// <summary>
/// Gets the total wall time that the subprocess took, in seconds.
/// </summary>
public float ExecutionTime {
get { return executionTime; }
}
/// <summary>
/// Gets the output that the subprocess wrote to its standard output stream.
/// </summary>
public string Stdout {
get { return stdout; }
}
/// <summary>
/// Gets the output that the subprocess wrote to its standard error stream.
/// </summary>
public string Stderr {
get { return stderr; }
}
/// <summary>
/// Gets the subprocess's exit code.
/// </summary>
public int ExitCode {
get { return exitCode; }
}
/// <summary>
/// Gets a flag indicating whether the subprocess was aborted because it
/// timed out.
/// </summary>
public bool TimedOut {
get { return timedOut; }
}
}
internal class ProcessStream {
/*
* Class to get process stdout/stderr streams
* Author: SeemabK ([email protected])
* Usage:
//create ProcessStream
ProcessStream myProcessStream = new ProcessStream();
//create and populate Process as needed
Process myProcess = new Process();
myProcess.StartInfo.FileName = "myexec.exe";
myProcess.StartInfo.Arguments = "-myargs";
//redirect stdout and/or stderr
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
//start Process
myProcess.Start();
//connect to ProcessStream
myProcessStream.Read(ref myProcess);
//wait for Process to end
myProcess.WaitForExit();
//get the captured output :)
string output = myProcessStream.StandardOutput;
string error = myProcessStream.StandardError;
*/
private Thread StandardOutputReader;
private Thread StandardErrorReader;
private Process RunProcess;
private string _StandardOutput = "";
private string _StandardError = "";
public string StandardOutput {
get { return _StandardOutput; }
}
public string StandardError {
get { return _StandardError; }
}
public ProcessStream() {
Init();
}
public void Read(Process process) {
try {
Init();
RunProcess = process;
if(RunProcess.StartInfo.RedirectStandardOutput) {
StandardOutputReader = new Thread(new ThreadStart(ReadStandardOutput));
StandardOutputReader.Start();
}
if(RunProcess.StartInfo.RedirectStandardError) {
StandardErrorReader = new Thread(new ThreadStart(ReadStandardError));
StandardErrorReader.Start();
}
int TIMEOUT = 1 * 60 * 1000; // one minute
if(StandardOutputReader != null)
StandardOutputReader.Join(TIMEOUT);
if(StandardErrorReader != null)
StandardErrorReader.Join(TIMEOUT);
} catch { }
}
private void ReadStandardOutput() {
if(RunProcess == null) return;
try {
StringBuilder sb = new StringBuilder();
string line = null;
while((line = RunProcess.StandardOutput.ReadLine()) != null) {
sb.Append(line);
sb.Append(Environment.NewLine);
}
_StandardOutput = sb.ToString();
} catch { }
}
private void ReadStandardError() {
if(RunProcess == null) return;
try {
StringBuilder sb = new StringBuilder();
string line = null;
while((line = RunProcess.StandardError.ReadLine()) != null) {
sb.Append(line);
sb.Append(Environment.NewLine);
}
_StandardError = sb.ToString();
} catch { }
}
private void Init() {
_StandardError = "";
_StandardOutput = "";
RunProcess = null;
Stop();
}
public void Stop() {
try { if(StandardOutputReader != null) StandardOutputReader.Abort(); } catch { }
try { if(StandardErrorReader != null) StandardErrorReader.Abort(); } catch { }
StandardOutputReader = null;
StandardErrorReader = null;
}
}
}
これは質問に答えませんが、 Registry.OpenRemoteBaseKey
メソッドは、REG
コマンドと同じ方法で別のマシンのレジストリに接続します。 RegistryKey.GetSubKeyNames
を呼び出して、REG QUERY
と同じ出力を取得します。
System.Diagnostics.Processクラスを使用して、StandardOutputとStandardErrorをキャプチャできます。
http://msdn.Microsoft.com/en-us/library/system.diagnostics.process.aspx
ドキュメントの備考セクションを必ずお読みください。 StandardOutputを使用できるようにするには、プロセスクラスの特定のプロパティを正しく設定する必要があります(たとえば、UseShellExecuteをfalseに設定する必要があります)。