0

我正在构建运行 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

4

1 回答 1

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...
}

注意:我只是在没有编译或任何认真验证的情况下输入了这个,所以请注意,大声笑......

于 2012-09-24T18:03:24.097 回答