是否可以运行任何基于命令行的程序或批处理文件和捕获器(重定向)输出到文本框LIVE
CL需要时间并产生文本!
类似于tracert.exe(需要时间并产生大量文本).
实际上我将使用tracert.exe,我喜欢实时捕获输出并在运行时在文本框中显示它
编辑:我的问题是让它生活我的意思是控制台产生的任何新行或字符将被发送/拉到/由textBox,直到程序完成!
我想构建的就像这个http://www.codeproject.com/KB/threads/redir.aspx (查看演示),但在C#中
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
Process pc = new Process();
pc.StartInfo.FileName = "tracert.exe";
pc.StartInfo.Arguments = "google.com";
pc.StartInfo.UseShellExecute = false;
pc.StartInfo.RedirectStandardOutput = true;
pc.StartInfo.CreateNoWindow = true;
pc.Start();
richTextBox1.Text = pc.StandardOutput.ReadToEnd();
pc.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)
编辑
在你的帮助下(非常感谢)和这个链接:http: //msdn.microsoft.com/query/dev10.query?appId = Dev10IDEF1&l = EN-US&k = k%28EHINVALIDOPERATION.WINFORMS.ILLEGALCROSSTHREADCALL%29; k% 28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV2.0%22%29; K-%28DevLang-CSHARP%29&RD =真
我用这段代码解决了(你认为它没问题吗?):
namespace GUIforCL2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Process _cmd;
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.richTextBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.richTextBox1.Text += text + Environment.NewLine;
}
}
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo cmdStartInfo = new ProcessStartInfo("tracert.exe");
cmdStartInfo.Arguments = "google.com";
cmdStartInfo.CreateNoWindow = true;
cmdStartInfo.RedirectStandardInput = true;
cmdStartInfo.RedirectStandardOutput = true;
cmdStartInfo.RedirectStandardError = true;
cmdStartInfo.UseShellExecute = false;
cmdStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_cmd = new Process();
_cmd.StartInfo = cmdStartInfo;
if (_cmd.Start())
{
_cmd.OutputDataReceived += new DataReceivedEventHandler(_cmd_OutputDataReceived);
_cmd.ErrorDataReceived += new DataReceivedEventHandler(_cmd_ErrorDataReceived);
_cmd.Exited += new EventHandler(_cmd_Exited);
_cmd.BeginOutputReadLine();
_cmd.BeginErrorReadLine();
}
else
{
_cmd = null;
}
}
void _cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
UpdateConsole(e.Data);
}
void _cmd_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
UpdateConsole(e.Data, Brushes.Red);
}
void _cmd_Exited(object sender, EventArgs e)
{
_cmd.OutputDataReceived -= new DataReceivedEventHandler(_cmd_OutputDataReceived);
_cmd.Exited -= new EventHandler(_cmd_Exited);
}
private void UpdateConsole(string text)
{
UpdateConsole(text, null);
}
private void UpdateConsole(string text, Brush color)
{
WriteLine(text, color);
}
private void WriteLine(string text, Brush color)
{
if (text != null)
{
SetText(text);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
创建Processtracert实例时,需要设置ProcessStartInfo.UseShellExecutetofalse和ProcessStartInfo.RedirectStandardOutputto true。这将允许您使用 Process.StandardOutput属性来读取输出。
您可以使用Process.BeginOutputReadLine来启动异步读取并向Process.OutputDataReceived.
更新:这是一个示例 WPF 程序,可以执行您想要的操作。
主窗口.xaml:
<Window x:Class="Console.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closed="Window_Closed">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer Name="outputViewer" SizeChanged="ScrollViewer_SizeChanged" >
<TextBlock Name="output" />
</ScrollViewer>
<TextBox Grid.Row="1" Name="input" KeyDown="input_KeyDown" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
主窗口.cs:
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace Console
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ProcessStartInfo cmdStartInfo = new ProcessStartInfo("cmd.exe");
cmdStartInfo.CreateNoWindow = true;
cmdStartInfo.RedirectStandardInput = true;
cmdStartInfo.RedirectStandardOutput = true;
cmdStartInfo.RedirectStandardError = true;
cmdStartInfo.UseShellExecute = false;
cmdStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_cmd = new Process();
_cmd.StartInfo = cmdStartInfo;
if (_cmd.Start() == true)
{
_cmd.OutputDataReceived += new DataReceivedEventHandler(_cmd_OutputDataReceived);
_cmd.ErrorDataReceived += new DataReceivedEventHandler(_cmd_ErrorDataReceived);
_cmd.Exited += new EventHandler(_cmd_Exited);
_cmd.BeginOutputReadLine();
_cmd.BeginErrorReadLine();
}
else
{
_cmd = null;
}
}
private void Window_Closed(object sender, EventArgs e)
{
if ((_cmd != null) &&
(_cmd.HasExited != true))
{
_cmd.CancelErrorRead();
_cmd.CancelOutputRead();
_cmd.Close();
_cmd.WaitForExit();
}
}
void _cmd_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
UpdateConsole(e.Data);
}
void _cmd_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
UpdateConsole(e.Data, Brushes.Red);
}
void _cmd_Exited(object sender, EventArgs e)
{
_cmd.OutputDataReceived -= new DataReceivedEventHandler(_cmd_OutputDataReceived);
_cmd.Exited -= new EventHandler(_cmd_Exited);
}
private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
{
outputViewer.ScrollToBottom();
}
private void input_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
_cmd.StandardInput.WriteLine(input.Text);
input.Text = "";
break;
case Key.Escape:
input.Text = "";
break;
}
}
private void UpdateConsole(string text)
{
UpdateConsole(text, null);
}
private void UpdateConsole(string text, Brush color)
{
if (!output.Dispatcher.CheckAccess())
{
output.Dispatcher.Invoke(
new Action(
() =>
{
WriteLine(text, color);
}
)
);
}
else
{
WriteLine(text, color);
}
}
private void WriteLine(string text, Brush color)
{
if (text != null)
{
Span line = new Span();
if (color != null)
{
line.Foreground = color;
}
foreach (string textLine in text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
line.Inlines.Add(new Run(textLine));
}
line.Inlines.Add(new LineBreak());
output.Inlines.Add(line);
}
}
Process _cmd;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5430 次 |
| 最近记录: |