使用命令行在启动过程后从cmd窗口接收文本

时间:2012-09-24 17:09:11

标签: c#

我是构建应用程序谁运行Wireshark并开始嗅探,Wireshark有dumpcap.exe文件谁接收参数(接口号,输出文件等)并开始嗅探,同时我可以在cmd窗口中看到数据包和这个数字一直在成长。 我的问题是如何每隔几秒钟捕获一次这个数字,以便在我的应用程序窗口中显示这个数字。

这是我的班级开始这个嗅探:

public class DumpPcap
{
    public int _interfaceNumber;
    public string _pcapPath;
    public string _dumPcapPath = @"C:\Program Files\Wireshark\dumpcap.exe";

    public DumpPcap(int interfaceNumber, string pcapPath)
    {
        _interfaceNumber = interfaceNumber;
        _pcapPath = pcapPath;
    }

    public void startTheCapture()
    {
        List<string> stList = new List<string>();
        ProcessStartInfo process = new ProcessStartInfo(_dumPcapPath);
        process.Arguments = string.Format("-i " + _interfaceNumber + " -s 65535 -w " + _pcapPath);
        process.WindowStyle = ProcessWindowStyle.Hidden;
        process.RedirectStandardOutput = true;
        process.RedirectStandardError = true;
        process.CreateNoWindow = true;
        process.UseShellExecute = false;
        process.ErrorDialog = false;
        Process dumpcap = Process.Start(process);
        StreamReader reader = dumpcap.StandardOutput;
        //dumpcap.WaitForExit(100000);

        while (!reader.EndOfStream)
        {
            stList.Add(reader.ReadLine());
        }
    }
}

这是截图,我用红色标记了我想要在我的应用程序中显示的字段:

http://image.torrent-invites.com/images/641Untitled.jpg

1 个答案:

答案 0 :(得分:0)

而不是尝试从ProcessStartInfo捕获输出文本,而不是从Process捕获输出文本,并通过OutputDataReceived事件处理程序拦截输出数据?

请尝试替换您的代码块:

List<string> stList = new List<string>();

var process = new Process();
process.StartInfo.FileName = _dumPcapPath;
process.StartInfo.Arguments = 
    string.Format("-i " + _interfaceNumber + " -s 65535 -w " + _pcapPath);
process.Startinfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Startinfo.RedirectStandardOutput = true;
process.Startinfo.RedirectStandardError = true;
process.Startinfo.CreateNoWindow = true;
process.Startinfo.UseShellExecute = false;
process.Startinfo.ErrorDialog = false;

// capture the data received event here...
process.OutputDataReceived += 
    new DataReceivedEventHandler(process_OutputDataReceived);

process.Start();
process.BeginOutputReadLine();


private void process_OutputDataReceived(object sender, DataReceivedEventArgs arg)
{
    // arg.Data contains the output data from the process...
}

注意:我只是在没有编译或任何严重验证的情况下键入此内容,因此请注意,大声笑......