private void Serialize2Disk(string FileName, bool Overwrite) { int nItemsCount = this.Items.Count; FileStream fs = null; bool bFileError = false; Cursor current = Cursor.Current; this.Cursor = Cursors.WaitCursor; if (nItemsCount >= 1) { DataLstView dlvItems = new DataLstView(); dlvItems.DataListViewItems = new ListViewItem[nItemsCount]; dlvItems.DataListViewTags = new object[nItemsCount]; this.Items.CopyTo(dlvItems.DataListViewItems, 0); dlvItems.ColumnNames = new string[this.Columns.Count]; dlvItems.ColumnAlignment = new byte[this.Columns.Count]; dlvItems.ColumnWidth = new int[this.Columns.Count]; for (int nLoopCnt = 0; nLoopCnt < this.Columns.Count; nLoopCnt++) { dlvItems.ColumnNames[nLoopCnt] = this.Columns[nLoopCnt].Text; dlvItems.ColumnAlignment[nLoopCnt] = (byte)this.Columns[nLoopCnt].TextAlign; dlvItems.ColumnWidth[nLoopCnt] = (int)this.Columns[nLoopCnt].Width; } for (int nLoopCnt = 0; nLoopCnt < nItemsCount; nLoopCnt++) { ListViewItem lvi = (ListViewItem)this.Items[nLoopCnt]; dlvItems.DataListViewTags[nLoopCnt] = lvi.Tag; } try { if (Overwrite) { fs = new FileStream(FileName, FileMode.OpenOrCreate); } else { fs = new FileStream(FileName, FileMode.Create); } } catch (ArgumentException) { bFileError = true; throw; } catch (FileNotFoundException) { bFileError = true; throw; } catch (IOException) { bFileError = true; throw; } if (!bFileError) { try { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, dlvItems); } catch (SerializationException) { throw; } catch (Exception) { throw; } finally { fs.Close(); } } this.Cursor = current; } this.Cursor = current; }
private void DeSerializeFrmDisk(string FileName) { FileStream fs = null; DataLstView dlvItems = new DataLstView(); bool bFileError = false; Cursor current = this.Cursor; this.Cursor = Cursors.WaitCursor; try { fs = new FileStream(FileName, FileMode.Open); } catch (ArgumentException) { bFileError = true; throw; } catch (FileNotFoundException) { bFileError = true; throw; } catch (IOException) { bFileError = true; throw; } if (!bFileError) { this.BeginUpdate(); try { BinaryFormatter bf = new BinaryFormatter(); this.DataSource = null; base.Columns.Clear(); this.mColumns.Clear(); this.Items.Clear(); dlvItems = (DataLstView)bf.Deserialize(fs); if (dlvItems.ColumnNames.Length >= 1) { for (int nLoopCnt = 0; nLoopCnt < dlvItems.ColumnNames.Length; nLoopCnt++) { base.Columns.Add(dlvItems.ColumnNames[nLoopCnt], dlvItems.ColumnWidth[nLoopCnt], (HorizontalAlignment)dlvItems.ColumnAlignment[nLoopCnt]); //base.Columns.Add(dlvItems.ColumnNames[nLoopCnt], -1, (HorizontalAlignment)dlvItems.ColumnAlignment[nLoopCnt]); this.mColumns.Add(dlvItems.ColumnNames[nLoopCnt], dlvItems.ColumnWidth[nLoopCnt]); } } base.Items.AddRange(dlvItems.DataListViewItems); if (dlvItems.DataListViewTags.Length >= 1) { for (int nLoopCnt = 0; nLoopCnt < dlvItems.DataListViewTags.Length; nLoopCnt++) { ListViewItem lvi = this.Items[nLoopCnt]; lvi.Tag = dlvItems.DataListViewTags[nLoopCnt]; } } } catch (SerializationException) { throw; } catch (Exception) { throw; } finally { fs.Close(); this.EndUpdate(); } } this.Cursor = current; if (this.Items.Count >= 1) { this.ResizeColumns(); } }