May 23, 2006

Storing long array from arraylist to bin file

Following snippet shows how to

1. get long array from array list
2. store long array in file

(Assumption file is created and _oBinWriter is binary writer created for file stream)

// convert arraylist to long arraylong[] tmpArray = new long[_myLongArrayList.Count];_myLongArrayList.CopyTo(tmpArray);

// save long array to file
byte[] bytes = new byte[_myLongArrayList.Count * 8]; // 8 = SizeOf(long)

GCHandle handle = GCHandle.Alloc(tmpArray, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
Marshal.Copy(ptr, bytes, 0, bytes.Length);handle.Free();

// finally write bytes in file.
_oBinWriter.Write(bytes);