static void Main(string[] args) { W_newModuleHeader WnMH = new W_newModuleHeader(); WnMH.id = 0; WnMH.index = 1; string _filePath = "BINARY.bin"; Console.Write("WnMH before saving" + "\nWnMH.id : " + WnMH.id + "\nWnMH.index : " + WnMH.index + "\n\n"); SaveToFile(_filePath, WnMH); WnMH.index = 0; Console.Write("WnMH before reading" + "\nWnMH.id : " + WnMH.id + "\nWnMH.index : " + WnMH.index + "\n\n"); WnMH = ReadFromFile(_filePath, WnMH.StructSize); Console.Write("WnMH after reading" + "\nWnMH.id : " + WnMH.id + "\nWnMH.index : " + WnMH.index); Console.ReadKey(); }
static public void SaveToFile(String _filePath, W_newModuleHeader WnMH) { byte[] buffer = null; try { FileStream stream = null; BinaryWriter writer = null; stream = new FileStream(_filePath, FileMode.Create); writer = new BinaryWriter(stream); buffer = new byte[WnMH.StructSize]; Marshal.Copy(WnMH.StructPointer, buffer, 0, WnMH.StructSize); writer.Write(buffer); // close writer.Close(); stream.Close(); } catch (Exception ex) { Console.Write("Save File Error\n" + ex.ToString()); } }