0

在我的应用程序中,我打开 Tshark 进程并开始捕获,并从我的主窗体中更新我的 UI。我的类属性之一是我的文件是如何创建的(pcap 文件),在我的 UI 上,我正在更新这个文件长度。我的问题是,如果这个文件是新的,则长度保持为零,但如果我的文件是已经存在的文件,我的 UI 会显示我正在进行的长度(当然文件被覆盖,但我可以看到它在增长)。

    public class Tshark
    {
        #region members
        private Process _tsharkProcess = new Process();
        private int _numberOfPackets;
        private string _tshark;
        private List<string> _list;
        private ProcessStartInfo _process;
        private int _interfaceNumber;
        private string _pcapPath;
        private string _status;
        private double _bitsPerSecond;
        private double _packetsPerSecond;
        private decimal _packetLimitSize;
        private DateTime _lastTimestamp;
        private PacketDevice _device;
        private FileInfo _file;
        private bool _processRunning;

        public void startCapturing()
        {            
            ThreadStart tStarter = delegate { openAdapterForStatistics(_device); };
            Thread thread = new Thread(tStarter);
            thread.IsBackground = true;
            thread.Start();
            ProcessStartInfo tsharkStartInfo = new ProcessStartInfo();
            tsharkStartInfo.FileName = _tshark; 
            tsharkStartInfo.RedirectStandardOutput = true; 
            tsharkStartInfo.RedirectStandardError = true; 
            tsharkStartInfo.RedirectStandardInput = true; 
            tsharkStartInfo.UseShellExecute = false; 
            tsharkStartInfo.CreateNoWindow = true; 
            tsharkStartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -s " + _packetLimitSize + " -w " + _pcapPath);
            _tsharkProcess.StartInfo = tsharkStartInfo;
            _tsharkProcess.ErrorDataReceived += _cmdProcess_ErrorDataReceived;
            _tsharkProcess.OutputDataReceived += tshark_OutputDataReceived; 
            _tsharkProcess.EnableRaisingEvents = true; 
            _tsharkProcess.Start();
            _processRunning = true;
            _tsharkProcess.BeginOutputReadLine(); 
            _tsharkProcess.BeginErrorReadLine();
_tsharkProcess.WaitForExit(); 
        }

    public long getFileLength()
    {
        _file = new FileInfo(_pcapPath);
        string directoryName = _file.DirectoryName;
        DirectoryInfo directoryInfo = new DirectoryInfo(directoryName);
        FileInfo[] dirs = directoryInfo.GetFiles();
        _file = dirs.FirstOrDefault(f => f.Name.Equals(_file.Name));

        if (_file != null)
        {

            return _file.Length;
        }

        return 0;
    }

        public void refreshFile()
        {            
            if (_file != null)
            {
                _file.Refresh();
            }
        }
}

从主窗体我通过计时器更新我的用户界面:

private void tmrUpdateLbl_Tick(object sender, EventArgs e)
{
    lblFileSizeTabSniffer2.Text = formatBytes(tshark.getFileLength());
    tshark.refreshFile();
}

在调试器中,我可以看到如果文件是新文件,则 _file 始终为空,因此在 getFileLength() 方法中返回零。

4

1 回答 1

0

您每次都需要获取完整的 FileInfo(即 in getFileLength)以获得最新的结果。将相关代码从startCapturing移至getFileLength.

于 2012-10-18T17:48:39.283 回答