Example #1
0
    private void btnConvertFile_Click(object sender, EventArgs e)
    {
        string pathFileNameText = Path.Combine(Application.StartupPath,
                                               "ZipCodes_1990Census_sortedAscending.txt");
        string input;

        if (File.Exists(pathFileNameText) == false)
        {
            MessageBox.Show(pathFileNameText + " does not exist",
                            Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        using (StreamReader sr = new StreamReader(pathFileNameText))
        {
            input = sr.ReadLine();
            while (input != null)
            {
                clsZipCode zipCode = new clsZipCode();
                zipCode.Deserialize(input);
                mZipCodeList.Add(zipCode);
                input = sr.ReadLine();
            }
            sr.Close();
        }

        string pathFileNameBinary = Path.Combine(Application.StartupPath, "ZipCodes_1990Census_sortedAscending.bin");

        using (FileStream fs = new FileStream(pathFileNameBinary, FileMode.Create))
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                foreach (clsZipCode zipCode in mZipCodeList)
                {
                    zipCode.BinarySerialize(bw);
                }
            }
        showMessage("A binary file of " + mZipCodeList.Count + " has been created");
    }