C# Sendkey到隐藏CMD
我最近创建了一个UI工具,其中包含一个重新创建的命令提示符。问题是,我没有办法杀死,例如,一个连续的ping。通常的方法是按CTRL+C终止正在运行的命令。我正在试图找到一种方法,从我的UI向隐藏的CMD发送一个按键,例如CTRL+C 所以你可以在脑海中想象这里发生了什么,我有一个侧面板,你可以在其中选择你想要在UI中使用的工具。我有一个丰富的文本框,名为C# Sendkey到隐藏CMD,c#,C#,我最近创建了一个UI工具,其中包含一个重新创建的命令提示符。问题是,我没有办法杀死,例如,一个连续的ping。通常的方法是按CTRL+C终止正在运行的命令。我正在试图找到一种方法,从我的UI向隐藏的CMD发送一个按键,例如CTRL+C 所以你可以在脑海中想象这里发生了什么,我有一个侧面板,你可以在其中选择你想要在UI中使用的工具。我有一个丰富的文本框,名为doAllThingsTextBox,我用它来做所有的事情。我还有另一个主要用于输入计算机名称的框,但我重新调整了它的用途,这样我就不必再制作
doAllThingsTextBox
,我用它来做所有的事情。我还有另一个主要用于输入计算机名称的框,但我重新调整了它的用途,这样我就不必再制作另一个文本框,它被称为CompNameBox
。如果我在CompNameBox中键入ping 8.8.8
,然后按enter键,它的工作方式类似于cmd,并在DoAllThingsTextBox中显示输出。假设我键入ping 8.8.8.8-t
。它将启动一个连续的ping,您可以通过按CTRL+C来停止。因为我没有直接在CMD中键入命令,所以一旦命令启动,我就无法停止它,除非我从task manager中终止它。但这会杀死我使用的整个cmd
我曾尝试使用sendkey代码,但无法确定如何将其发送到正在运行的cmd on按钮
private void ToolSelectionBox_SelectedIndexChanged(object sender, EventArgs e)
{
//Display Panel
if (ToolSelectionBox.SelectedItem != null && ToolSelectionBox.SelectedItem.ToString() == "Command Prompt")
{
CmdPrompt = new Process();
CmdPrompt.StartInfo.FileName = "cmd.exe";
CmdPrompt.StartInfo.CreateNoWindow = true;
CmdPrompt.StartInfo.UseShellExecute = false;
CmdPrompt.StartInfo.RedirectStandardOutput = true;
CmdPrompt.StartInfo.RedirectStandardError = true;
CmdPrompt.StartInfo.RedirectStandardInput = true;
CmdOutput = new StringBuilder("");
CmdPrompt.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
CmdPrompt.ErrorDataReceived += new DataReceivedEventHandler(SortErrorHandler);
CmdPrompt.Start();
CmdPrompt.BeginOutputReadLine();
while (!CmdPrompt.HasExited)
{
Application.DoEvents();
}
}
}
private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
if (DoAllTheThingsButton.Visible == false)
{
//This is to check if a button used for other function is the UI is visible
//If it isn't then you are likely using the cmd option.
CmdPrompt.StandardInput.WriteLine(CompNameBox.Text);
CompNameBox.Text = "";
}
else
{
//Do nothing
}
}
}
private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (DoAllTheThingsTextBox.InvokeRequired) { DoAllTheThingsTextBox.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new[] { sendingProcess, outLine }); }
else
{
DoAllTheThingsTextBox.AppendText(Environment.NewLine + outLine.Data);
DoAllTheThingsTextBox.SelectionStart = DoAllTheThingsTextBox.TextLength;
DoAllTheThingsTextBox.ScrollToCaret();
}
}
private void SortErrorHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (DoAllTheThingsTextBox.InvokeRequired) { DoAllTheThingsTextBox.BeginInvoke(new DataReceivedEventHandler(SortErrorHandler), new[] { sendingProcess, outLine }); }
else
{
DoAllTheThingsTextBox.AppendText(Environment.NewLine + outLine.Data);
DoAllTheThingsTextBox.SelectionStart = DoAllTheThingsTextBox.TextLength;
DoAllTheThingsTextBox.ScrollToCaret();
}
}