/// <summary> /// Gets the header from a dbf file. /// </summary> /// <param name="dbfFile">The DBF file.</param> /// <returns></returns> public static dBaseHeader GetHeader(string dbfFile) { if (!File.Exists(dbfFile)) throw new FileNotFoundException(dbfFile + " not found"); dBaseHeader header = new dBaseHeader(); header.ReadHeader(new BinaryReader(new FileStream(dbfFile, FileMode.Open, FileAccess.Read, FileShare.Read))); return header; }
/// <summary> /// This will return a copy of the index, but also preserve /// a master copy in this object that is read-only. /// </summary> /// <param name="filename">The filename of the dbf file to index</param> /// <returns>returns a List of long offsets. The index in the list is the row value.</returns> public List<long> GetIndex(string filename) { List<long> OffsetsCopy = new List<long>(); _offsets = new List<long>(); if (filename == null) { throw new ArgumentNullException(filename); } // check for the file existing here, otherwise we will not get an error //until we read the first record or read the header. if (!System.IO.File.Exists(filename)) { throw new System.IO.FileNotFoundException(String.Format("Could not find file \"{0}\"", filename)); } _filename = filename; _header = new dBaseHeader(); // read the header System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader binaryreader = new System.IO.BinaryReader(stream); _header.ReadHeader(binaryreader); int iCurrentRecord = 0; long CurrentOffset = _header.HeaderLength; byte DeleteChar = Convert.ToByte('*'); int RowLength = _header.RecordLength; while (iCurrentRecord < _header.NumRecords) { int Val = stream.ReadByte(); if (stream.ReadByte() != DeleteChar) { _offsets.Add(CurrentOffset); OffsetsCopy.Add(CurrentOffset); iCurrentRecord++; } stream.Seek(RowLength - 1, System.IO.SeekOrigin.Current); CurrentOffset += RowLength; } binaryreader.Close(); stream.Close(); binaryreader.Close(); return OffsetsCopy; }
/// <summary> /// Gets the header information for the dbase file. /// </summary> /// <returns>DbaseFileHeader contain header and field information.</returns> public dBaseHeader GetHeader() { if (_header==null) { FileStream stream = new FileStream(_filename, System.IO.FileMode.Open); BinaryReader dbfStream = new BinaryReader(stream); _header = new dBaseHeader(); // read the header _header.ReadHeader(dbfStream); dbfStream.Close(); stream.Close(); } return _header; }