/// <summary> /// 清理所有正在使用的资源。 /// </summary> protected virtual void Dispose(bool disposing) { if (disposing) { this._recordBuffer = null; this._dbfHeader = null; this._dbfFields = null; if (this._isFileOpened && null != this._fileStream) { this._fileStream.Close(); this._binaryReader.Close(); } this._fileStream = null; this._binaryReader = null; this._isFileOpened = false; this._fieldCount = 0; this._recordCount = 0; this._recordIndex = -1; } }
/// <summary> /// 从dbf文件中读取文件头信息 /// </summary> /// <returns></returns> private bool ReadFileHeader() { if (!this._isFileOpened) // 确认目标文件已经打开 { return(false); } if (null == this._dbfHeader) // 尝试构造新的dbf文件头对象 { this._dbfHeader = new DBFHeader(); } try { this._dbfHeader.Version = this._binaryReader.ReadSByte(); //第1字节 this._dbfHeader.LastModifyYear = this._binaryReader.ReadByte(); //第2字节 this._dbfHeader.LastModifyMonth = this._binaryReader.ReadByte(); //第3字节 this._dbfHeader.LastModifyDay = this._binaryReader.ReadByte(); //第4字节 this._dbfHeader.RecordCount = this._binaryReader.ReadUInt32(); //第5-8字节 this._dbfHeader.HeaderLength = this._binaryReader.ReadUInt16(); //第9-10字节 this._dbfHeader.RecordLength = this._binaryReader.ReadUInt16(); //第11-12字节 this._dbfHeader.Reserved = this._binaryReader.ReadBytes(16); //第13-14字节 this._dbfHeader.TableFlag = this._binaryReader.ReadSByte(); //第15字节 this._dbfHeader.CodePageFlag = this._binaryReader.ReadSByte(); //第16字节 this._dbfHeader.Reserved2 = this._binaryReader.ReadBytes(2); ////第17-18字节 } catch (Exception e) { throw e; } // 设置记录数目 this._recordCount = this._dbfHeader.RecordCount; uint fieldCount = (uint)((this._dbfHeader.HeaderLength - DBFHeader.DBFHeaderSize - 1) / DBFField.DBFFieldSize); this._fieldCount = 0; // 由于有些dbf文件的文件头最后有附加区段,但是有些文件没有,在此使用笨方法计算字段数目 // 就是测试每一个存储字段结构区域的第一个字节的值,如果不为0x0D,表示存在一个字段 // 否则从此处开始不再存在字段信息 try { for (uint i = 0; i < fieldCount; i++) { // 定位到每个字段结构区,获取第一个字节的值 this._fileStream.Seek(DBFHeader.DBFHeaderSize + i * DBFField.DBFFieldSize, SeekOrigin.Begin); byte flag = this._binaryReader.ReadByte(); // 如果获取到的标志不为0x0D,则表示该字段存在;否则从此处开始后面再没有字段信息 if (0x0D != flag) { this._fieldCount++; } else { break; } } } catch (Exception e) { throw e; } return(true); }