예제 #1
0
        private static List <Person> LoadFromBinaryFile()
        {
            if (File.Exists(BinaryFile))
            {
                BinaryReader  binaryreader = new BinaryReader(new FileStream(BinaryFile, FileMode.Open));
                int           limit        = binaryreader.ReadInt32();
                List <Person> newList      = new List <Person>();

                for (int i = 0; i < limit; i++)
                {
                    string name = binaryreader.ReadString();

                    int      year  = binaryreader.ReadInt32();
                    int      month = binaryreader.ReadInt32();
                    int      day   = binaryreader.ReadInt32();
                    DateTime bday  = new DateTime(year, month, day);

                    string gender   = binaryreader.ReadString();
                    string eyecolor = binaryreader.ReadString();

                    string   haircolor  = binaryreader.ReadString();
                    float    hairlength = binaryreader.ReadSingle();//Reads in float
                    HairData hair       = new HairData(haircolor, hairlength);

                    Person newp = new Person(name, bday, gender, eyecolor, hair);
                    newList.Add(newp);
                }
                binaryreader.Close();
                return(newList);
            }
            return(null);
        }
예제 #2
0
        private static void SaveToBinaryFile(List <Person> plist)
        {
            if (plist.Count == 0)
            {
                return;
            }
            //BinaryFile
            BinaryWriter binarywriter = new BinaryWriter(new FileStream(BinaryFile, FileMode.Create));

            binarywriter.Write(plist.Count); //int
            foreach (Person pitem in plist)
            {
                binarywriter.Write(pitem.Name);//string
                //
                DateTime dt = pitem.Birthday;
                binarywriter.Write(dt.Year);        //int
                binarywriter.Write(dt.Month);       //int
                binarywriter.Write(dt.Day);         //int

                binarywriter.Write(pitem.Gender);   //string
                binarywriter.Write(pitem.EyeColor); //string

                HairData hd = pitem.Hair;
                binarywriter.Write(hd.Color);  //string
                binarywriter.Write(hd.Lenght); //float
            }
            binarywriter.Flush();
            binarywriter.Close();
        }
예제 #3
0
 public Person(string name, DateTime birthday, string gender, string eyecolor, HairData hdata)
 {
     Name     = name;
     Birthday = birthday;
     Gender   = gender;
     EyeColor = eyecolor;
     privHair = hdata;
 }
예제 #4
0
 public void SetHair(HairData h)
 {
     privHair = h;
 }