/ /ユーザーが入力しているかのようにコンソールに入力を送信するにはどうすればよいですか? -.net、自動化、cmd、stdin

ユーザーが入力しているかのように入力をコンソールに送信する方法は? - .net、オートメーション、cmd、stdin

これが私の問題です。TTYで実行する必要のあるプログラムがあります。cygwinがこのTTYを提供します。 stdInをリダイレクトすると、TTYがないため、プログラムが失敗します。このプログラムを変更することはできません。自動化する方法が必要です。

cmd.exeウィンドウを取得してデータを送信し、ユーザーが入力していると思わせるにはどうすればよいですか?

私はC#を使用しています。java.awt.Robotでそれを行う方法があると思いますが、他の理由でC#を使用する必要があります。

回答:

回答№1は1

これは、 SendKeys()。それはC#ではなくVBScriptですが、それでも-あなたは 一部 それを自動化する方法:

Set Shell = CreateObject("WScript.Shell")

Shell.Run "cmd.exe /k title RemoteControlShell"
WScript.Sleep 250

Shell.AppActivate "RemoteControlShell"
WScript.Sleep 250

Shell.SendKeys "dir{ENTER}"

回答№2のための5

入力をコンソールに送信する方法を理解しました。ジョン・スキートが言ったことを使いました。これがこれを実装する正しい方法であるかどうかは100%わかりません。

これを改善するためのコメントがあれば、ここに行きたいと思います。私はそれを理解できるかどうかを確認するためだけにこれを行いました。

これが私が見つめたプログラムで、ユーザーからの入力を待っていました。

class Program
{
static void Main(string[] args)
{
// This is needed to wait for the other process to wire up.
System.Threading.Thread.Sleep(2000);

Console.WriteLine("Enter Pharse: ");

string pharse = Console.ReadLine();

Console.WriteLine("The password is "{0}"", pharse);


Console.WriteLine("Press any key to exit. . .");
string lastLine = Console.ReadLine();

Console.WriteLine("Last Line is: "{0}"", lastLine);
}
}

これは、もう一方に書き込むコンソールアプリです。

class Program
{
static void Main(string[] args)
{
// Find the path of the Console to start
string readFilePath = System.IO.Path.GetFullPath(@"......ReadingConsolebinDebugReadingConsole.exe");

ProcessStartInfo startInfo = new ProcessStartInfo(readFilePath);

startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;

Process readProcess = new Process();
readProcess.StartInfo = startInfo;

// This is the key to send data to the server that I found
readProcess.OutputDataReceived += new DataReceivedEventHandler(readProcess_OutputDataReceived);

// Start the process
readProcess.Start();

readProcess.BeginOutputReadLine();

// Wait for other process to spin up
System.Threading.Thread.Sleep(5000);

// Send Hello World
readProcess.StandardInput.WriteLine("Hello World");

readProcess.StandardInput.WriteLine("Exit");

readProcess.WaitForExit();
}

static void readProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
// Write what was sent in the event
Console.WriteLine("Data Recieved at {1}: {0}", e.Data, DateTime.UtcNow.Ticks);
}
}

回答№3の場合は1

を使用して、コード内でプログラム(またはcygwin)を起動できますか? ProcessStartInfo.RedirectStandardInput (および出力/エラー)データフローを制御しますか?


回答№4の場合は0

私はしばらく前に同様の問題を抱えていました、cygwinいくつかの有用な情報(正確なcygwin関数、エラーテキスト、およびWINAPIエラーコード)をエラーストリームに書き込む必要があります。それをどこかにリダイレクトして、書き込む内容を読み取る必要があります。