public static void save(String name, ComboBox history) { if (!File.Exists(historyFile)) { File.Create(historyFile).Close(); } byte[] bytes = File.ReadAllBytes(historyFile); ReadBuffer rb = new ReadBuffer(bytes); int sections = rb.readByte(); bool foundSector = false; WriteBuffer wb = new WriteBuffer(); wb.writeByte(sections); if (sections >= 0) { for (int i = 0; i < sections; i++) { String sectorName = rb.readString(); wb.writeString(sectorName); int sectorSize = rb.readInt(); if (sectorName != name) // Not our sector, copy { wb.writeInt(sectorSize); for (int o = 0; o < sectorSize; o++) { wb.writeString(rb.readString()); } } else // Our sector, insert our data // Empty previous data { for (int o = 0; o < sectorSize; o++) { rb.readString(); } // Insert new data int dataSize = history.Items.Count > historySize ? historySize : history.Items.Count; wb.writeInt(dataSize); for (int o = 0; o < dataSize; o++) { wb.writeString(history.Items[o].ToString()); } foundSector = true; } } } else { sections = 0; } if (!foundSector) { wb.writeString(name); int dataSize = history.Items.Count > historySize ? historySize : history.Items.Count; wb.writeInt(dataSize); for (int o = 0; o < dataSize; o++) { wb.writeString(history.Items[o].ToString()); } sections++; } byte[] data = wb.ToArray(); data[0] = (byte)sections; File.WriteAllBytes(historyFile, data); }
public static void save(String name, ComboBox history) { try { if (!File.Exists(historyFile)) { File.Create(historyFile).Close(); } byte[] bytes = File.ReadAllBytes(historyFile); ReadBuffer rb = new ReadBuffer(bytes); int sections = 0; try { sections = rb.readByte(); } catch (OutOfBoundsReadException) { // No sections } bool foundSector = false; WriteBuffer wb = new WriteBuffer(); wb.writeByte(sections); if (sections >= 0) { for (int i = 0; i < sections; i++) { String sectorName = rb.readString(Settings.defaultEncoding); wb.writeString(sectorName, Settings.defaultEncoding); int sectorSize = rb.readInt(); if (sectorName != name || history == null) // Not our sector, copy (or not saving a sector) { wb.writeInt(sectorSize); for (int o = 0; o < sectorSize; o++) { wb.writeByteArray(rb.readByteArray()); } } else // Our sector, insert our data // Empty previous data { for (int o = 0; o < sectorSize; o++) { rb.readByteArray(); } // Insert new data int dataSize = history.Items.Count > historySize ? historySize : history.Items.Count; wb.writeInt(dataSize); for (int o = 0; o < dataSize; o++) { wb.writeString(history.Items[o].ToString(), Settings.defaultEncoding); } foundSector = true; } } } else { sections = 0; } if (!foundSector && history != null) { wb.writeString(name, Settings.defaultEncoding); int dataSize = history.Items.Count > historySize ? historySize : history.Items.Count; wb.writeInt(dataSize); for (int o = 0; o < dataSize; o++) { wb.writeString(history.Items[o].ToString(), Settings.defaultEncoding); } sections++; } _storageNoLoad.write(wb); byte[] data = wb.ToArray(); data[0] = (byte)sections; File.WriteAllBytes(historyFile, data); } catch (OutOfBoundsReadException) { // Pass read beyond return; } }