【发布时间】:2014-01-08 09:49:20
【问题描述】:
我正在尝试这样做:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("git config --global user.name \"My Name\"");
sw.WriteLine("git config --global user.email \"my email\"");
sw.WriteLine("call start-ssh-agent");
sw.WriteLine("ssh-add c:\\temp\\ServerPull.ppk");
System.Threading.Thread.Sleep(10000);
sw.WriteLine(Environment.NewLine);
sw.WriteLine("git clone git@github.com:myrepo");
}
}
Console.WriteLine("Done");
}
问题是“ssh-add”命令需要输入密码,而我不能让 C# 输入它。 Thread.Sleep 之后的任何其他命令都会进入缓冲区,直到我自己在 CMD 框中实际输入内容。
Console.Writeline() 输出到同一个框,但实际上并没有被“输入”
编辑: 为清楚起见,我不打算执行 Console.ReadLine() 或实际获取用户的输入。这些命令是我需要它们的方式,但我需要自动将字符串发送到另一个要求输入密码的应用程序(ssh-add)。通过 sw.WriteLine 编写密码短语不起作用,因为控制台在等待输入时不会执行任何代码。
编辑: 在编写我的第一个编辑时,我对来自 cmets 的一些代码建议产生了灵感。现在结束了:
const int VK_RETURN = 0x0D;
const int WM_KEYDOWN = 0x100;
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("git config --global user.name \"my name\"");
sw.WriteLine("git config --global user.email \"my email\"");
sw.WriteLine("call start-ssh-agent");
var enterThread = new Thread(
new ThreadStart(
() =>
{
Thread.Sleep(10000);
var hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0);
}
));
enterThread.Start();
sw.WriteLine("ssh-add c:\\temp\\ServerPull.ppk");
sw.WriteLine("git clone myrepo");
sw.WriteLine("ping 127.0.0.1");
}
}
Console.WriteLine("Done");
}
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
延迟后发送回车键。我应该能够从那里修改它以发送我需要的任何东西。
【问题讨论】: