1

我使用命令行打开 WireShark 并开始捕获数据包,当我使用 CMD 窗口执行此操作时,我可以看到传入数据包的数量以及我想在我的应用程序表单(win 表单)中显示的这个数字,目前这是我的代码但我的应用程序因错误而崩溃

static void Main(string[] args)
{
    try
    {
        string _pcapPath = @"C:\test.pcap";
        Process _tsharkProcess = new Process();
        _tsharkProcess.StartInfo.FileName = @"C:\Program Files\Wireshark\tshark.exe";
        _tsharkProcess.StartInfo.Arguments = string.Format(" -i " + 2 + " -c " + int.MaxValue + " -w " + _pcapPath);
        _tsharkProcess.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
        _tsharkProcess.StartInfo.RedirectStandardOutput = true;
        _tsharkProcess.StartInfo.UseShellExecute = false;
        //_tsharkProcess.StartInfo.CreateNoWindow = true;
        //_tsharkProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        _tsharkProcess.Start();
        StreamReader myStreamReader = _tsharkProcess.StandardOutput;
        string myString = myStreamReader.ReadLine(); //read the standard output of the spawned process. 
        Console.WriteLine(myString);
        _tsharkProcess.WaitForExit();
    }
    catch (Exception)
    {

    }

}

private static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    string srt = e.Data; //arg.Data contains the output data from the process...  
}
4

1 回答 1

2

您可以尝试使用此代码

注意:在开始之前设置这些行

            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();

代码:

            Process process = new Process();
            process.StartInfo.FileName = @"C:\Program Files\Wireshark\tshark.exe";
            process.StartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -c " + int.MaxValue + " -w " + _pcapPath);
            process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);

            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;


       process.Start();

            StreamReader myStreamReader = process.StandardOutput;
            // Read the standard output of the spawned process. 
            string myString = myStreamReader.ReadLine();
            Console.WriteLine(myString);

            process.WaitForExit();
            process.Close();
        }
于 2012-09-25T14:05:48.757 回答