这是我的问题。我有一个程序必须在 TTY 中运行,cygwin 提供了这个 TTY。当我重定向 stdIn 时,程序失败,因为它没有 TTY。我无法修改该程序,并且需要某种方法使其自动化。
如何抓取 cmd.exe 窗口并向其发送数据并使其认为用户正在键入它?
我正在使用 C#,我相信有一种方法可以使用 java.awt.Robot 来完成此操作,但由于其他原因我必须使用 C#。
我已经弄清楚如何将输入发送到控制台。我用的是乔恩·斯基特所说的。我不能 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(@"..\..\..\ReadingConsole\bin\Debug\ReadingConsole.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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)