please HELLPPPP...
-
I have to make an application to write and read structures! I have to do it in c#! I know to do this in c/c++ but i have no idea how to do it in c#! for example, my structure is like this: (in C) struct Info { int password; string name; etc. }X; ( i used File *f, fopen etc..) how to write this into a file (*.dat) and the read them...in c# I found this example: http://www.codeproject.com/useritems/readwritestructstobinfile.asp but it seems to be difficult and there are some saying that is a terrible idea!
-
I have to make an application to write and read structures! I have to do it in c#! I know to do this in c/c++ but i have no idea how to do it in c#! for example, my structure is like this: (in C) struct Info { int password; string name; etc. }X; ( i used File *f, fopen etc..) how to write this into a file (*.dat) and the read them...in c# I found this example: http://www.codeproject.com/useritems/readwritestructstobinfile.asp but it seems to be difficult and there are some saying that is a terrible idea!
Hi, add this using statement to your code:
using System.Runtime.Serialization.Formatters.Binary;
use this code to read the file:MyClass MyObject = null; BinaryFormatter binReader = new BinaryFormatter(); System.IO.Stream readStream = File.Open("c:\blah.dat", FileMode.Open); MyObject = (MyClass)binReader.Deserialize(readStream); readStream.Close();
and this one to write it:MyClass MyObject = new MyClass(); BinaryFormatter binWriter = new BinaryFormatter(); System.IO.Stream writeStream = File.Open("c:\blah.dat", FileMode.Create); binWriter.Serialize(writeStream, MyObject); writeStream.Close();
and this is the class (or struct) you want to write to a file...[Serializable] public class MyClass { string SomeString = null; int SomeInteger = 0; string AnotherString = null; }