public bool Create(string filename, List <ColumnInfo> fields) { FileMode mode = FileMode.Create; FileAccess access = FileAccess.ReadWrite; // exclusive string filename_memo = string.Empty; bool memo = false; foreach (ColumnInfo item in fields) { memo = memo || (Convert.Type(item.DataType, item.Length) == DataType.Memo); } FileStream stream = new FileStream(filename, mode, access); FileStream stream_memo = null; bool ok = (stream != null); if (ok && memo) { filename_memo = MemoFile.CreateFileName(filename); stream_memo = new FileStream(filename_memo, mode, access); ok = (stream_memo != null); } if (ok) { byte[] bytes; ok = Attach(stream, filename, false); if (ok) { HeaderLength = Const.DefaultHeaderLen + Const.FieldTerminatorLen + Const.DefaultFieldLen * fields.Count; IsDirty = true; stream.SetLength(HeaderLength); StreamSeek(stream, Const.DefaultHeaderLen); Columns = fields; RecordLength = Const.RECORD_POS_DATA; foreach (ColumnInfo item in fields) { DBF_FILEFIELD_3 field = new DBF_FILEFIELD_3(); if (item.Name.Length > Const.FieldNameLen) { throw new Exception("Name too long"); } field.title = item.Name; field.type = Const.DataTypes[(int)Convert.Type(item.DataType, item.Length)]; field.length = (byte)item.Length; field.deccount = (byte)item.DecCount; bytes = Utility.StructureToPtr <DBF_FILEFIELD_3>(field); StreamWrite(stream, bytes); RecordLength += item.Length; } bytes = new byte[] { (byte)Const.FieldTerminator }; StreamWrite(stream, bytes); } if (stream_memo != null) { AttachMemo(stream_memo, filename_memo); } } return(ok); }
public bool Open(string filename, FileMode mode) { FileAccess access = (mode == FileMode.Open) ? FileAccess.Read // non-exclusive : FileAccess.ReadWrite; // exclusive var stream = new FileStream(filename, mode, access); bool ok = (stream != null); if (ok) { ok = Attach(stream, filename, true); if (ok) { bool memo = false; foreach (ColumnInfo item in Columns) { memo = memo || (Convert.Type(item.DataType, item.Length) == DataType.Memo); } if (memo) { string filename_memo = MemoFile.CreateFileName(filename); if (System.IO.File.Exists(filename_memo)) { var stream_memo = new FileStream(filename_memo, mode, access); ok = (stream_memo != null); if (ok) { ok = AttachMemo(stream_memo, filename_memo); } } if (!ok) { Close(); } } } else { stream.Close(); } } return(ok); }