using System; using System.IO; using System.Text; namespace RandomFileAccess { class Test { [STAThread] static void Main(string[] args) { myName me = new myName("help"); FileStream fs = File.Create("DB.bin"); BinaryWriter bw = new BinaryWriter(fs, Encoding.Unicode); //<--------- bw.Write(me.Name.ToCharArray()); //<--------- Writing data as Chararray bw.Flush(); fs.Close(); } } class myName { private const int CHAR_ARRAY_LENGTH = 10; private char[] _name = new char[CHAR_ARRAY_LENGTH]; public myName(string sName) { Name = sName; } public string Name { set { string name = value; int len = name.Length; if (len < CHAR_ARRAY_LENGTH) { _name = name.PadRight(CHAR_ARRAY_LENGTH, ' ').ToCharArray(); } else if (len >CHAR_ARRAY_LENGTH) { _name = name.Substring(0, CHAR_ARRAY_LENGTH).ToCharArray(); } else { _name = name.ToCharArray(); } } get { return new string(_name); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)