예제 #1
0
        private int GetDistance()
        {
            FileStream      stream = new FileStream(fileName, FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            int             startDay = -1, finishDay = -1, count = 0;

            while (stream.Position < stream.Length)
            {
                AverageTemp readData = (AverageTemp)bf.Deserialize(stream); // восстановление объекта

                if (readData.Temperature < 0)
                {
                    if (startDay == -1)
                    {
                        startDay = count;
                    }

                    if (count >= startDay)
                    {
                        finishDay = count;
                    }
                }
                count++;
            }
            stream.Close();

            int distance = 0;

            if (finishDay > startDay)
            {
                distance = finishDay - startDay - 1;
            }
            return(distance);
        }
예제 #2
0
        /// <summary>
        /// Добавление в заданную позицию
        /// </summary>
        private void AddToFileByIndex(AverageTemp averageTemp, int index)
        {
            FileStream      temp = new FileStream("Temp.bin", FileMode.Create);
            BinaryFormatter bf   = new BinaryFormatter();



            FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate);

            stream.Position = 0;
            int count = 0;

            while (stream.Position < stream.Length)
            {
                AverageTemp obj = (AverageTemp)bf.Deserialize(stream); // восстановление объекта

                if (count == index - 1)
                {
                    bf.Serialize(temp, averageTemp);
                }

                if (!obj.Deleted)
                {
                    bf.Serialize(temp, obj); // сохранение объекта  в потоке f
                    count++;
                }
            }
            stream.Close();
            temp.Close();

            File.Delete(fileName);
            File.Move("Temp.bin", fileName);
            File.Delete("Temp.bin");
        }
예제 #3
0
        private void EditByIndex(int index)
        {
            FileStream      stream   = new FileStream(fileName, FileMode.Open);
            BinaryFormatter bf       = new BinaryFormatter();
            AverageTemp     obj      = null;
            int             count    = 0;
            long            position = 0;

            while (stream.Position < stream.Length)
            {
                position = stream.Position;
                obj      = (AverageTemp)bf.Deserialize(stream); // восстановление объекта

                if (listBox1.SelectedIndex == count)
                {
                    break;
                }

                count++;
            }

            EditForm editForm = new EditForm(obj);

            DialogResult dr = editForm.ShowDialog();

            if (dr == DialogResult.OK)
            {
                stream.Position = position;
                bf.Serialize(stream, editForm.AverageTemp); // сохранение объекта  в потоке
                listBox1.Items[index] = editForm.AverageTemp;
            }
            stream.Close();
        }
예제 #4
0
        private void DeleteFromFileByKey(int key)
        {
            FileStream      stream = new FileStream(fileName, FileMode.Open);
            BinaryFormatter bf     = new BinaryFormatter();

            int count        = 0;
            int index        = 0;
            int deletedCount = 0;

            while (stream.Position < stream.Length)
            {
                long        startPosition = stream.Position;
                AverageTemp obj           = (AverageTemp)bf.Deserialize(stream); // восстановление объекта

                if (obj.Deleted)
                {
                    deletedCount++;
                }
                else
                {
                    if (obj.NumDay == key)
                    {
                        obj.Deleted     = true;
                        stream.Position = startPosition;
                        bf.Serialize(stream, obj); // сохранение объекта  в потоке f
                        deletedCount++;
                    }
                    index++;
                }

                count++;
            }

            if (deletedCount > count / 2)
            {
                FileStream temp = new FileStream("Temp.bin", FileMode.Create);
                stream.Position = 0;

                while (stream.Position < stream.Length)
                {
                    AverageTemp obj = (AverageTemp)bf.Deserialize(stream); // восстановление объекта
                    if (!obj.Deleted)
                    {
                        bf.Serialize(temp, obj); // сохранение объекта  в потоке f
                    }
                }
                temp.Close();
                stream.Close();

                File.Delete(fileName);
                File.Move("Temp.bin", fileName);
                File.Delete("Temp.bin");
            }
            else
            {
                MessageBox.Show("Запись с указанным ключом не найдена.", "Редактирование по ключу", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            stream.Close();
        }
예제 #5
0
        private void DeleteFromFileByIndex(int indexForDelete)
        {
            FileStream      stream = new FileStream(fileName, FileMode.Open);
            BinaryFormatter bf     = new BinaryFormatter();

            int count        = 0;
            int index        = 0;
            int deletedCount = 0;

            while (stream.Position < stream.Length)
            {
                long        startPosition = stream.Position;
                AverageTemp obj           = (AverageTemp)bf.Deserialize(stream); // восстановление объекта

                if (obj.Deleted)
                {
                    deletedCount++;
                }
                else
                {
                    if (index == indexForDelete)
                    {
                        obj.Deleted     = true;
                        stream.Position = startPosition;
                        bf.Serialize(stream, obj); // сохранение объекта  в потоке f
                        deletedCount++;
                    }
                    index++;
                }

                count++;
            }

            if (deletedCount > count / 2)
            {
                FileStream temp = new FileStream("Temp.bin", FileMode.Create);
                stream.Position = 0;

                while (stream.Position < stream.Length)
                {
                    AverageTemp obj = (AverageTemp)bf.Deserialize(stream); // восстановление объекта
                    if (!obj.Deleted)
                    {
                        bf.Serialize(temp, obj); // сохранение объекта  в потоке f
                    }
                }
                temp.Close();
                stream.Close();

                File.Delete(fileName);
                File.Move("Temp.bin", fileName);
                File.Delete("Temp.bin");
            }

            stream.Close();
        }
예제 #6
0
        /// <summary>
        /// Добавление в конец
        /// </summary>
        /// <param name="averageTemp"></param>
        private void AddToEndOfFile(AverageTemp averageTemp)
        {
            FileStream f1 = new FileStream(fileName, FileMode.OpenOrCreate);

            f1.Position = f1.Length;
            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(f1, averageTemp); // сохранение объекта  в потоке f
            f1.Close();
        }
예제 #7
0
        private void EditByKey(int key)
        {
            FileStream      stream   = new FileStream(fileName, FileMode.Open);
            BinaryFormatter bf       = new BinaryFormatter();
            AverageTemp     obj      = null;
            int             index    = 0;
            long            position = 0;

            while (stream.Position < stream.Length)
            {
                position = stream.Position;
                obj      = (AverageTemp)bf.Deserialize(stream); // восстановление объекта

                if (obj.Deleted)
                {
                    continue;
                }

                if (obj.NumDay == key)
                {
                    break;
                }

                index++;
            }

            if (index < listBox1.Items.Count)
            {
                EditForm editForm = new EditForm(obj);

                DialogResult dr = editForm.ShowDialog();

                if (dr == DialogResult.OK)
                {
                    stream.Position = position;
                    bf.Serialize(stream, editForm.AverageTemp); // сохранение объекта  в потоке
                    listBox1.Items[index] = editForm.AverageTemp;
                }
            }
            else
            {
                MessageBox.Show("Запись с указанным ключом не найдена.", "Редактирование по ключу", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            stream.Close();
        }
예제 #8
0
        private double GetAverageTemperature()
        {
            FileStream      stream = new FileStream(fileName, FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            double          average = 0, sum = 0;
            int             count = 0;

            while (stream.Position < stream.Length)
            {
                AverageTemp d = (AverageTemp)bf.Deserialize(stream); // восстановление объекта
                sum = sum + d.Temperature;
                count++;
            }
            stream.Close();

            if (count != 0)
            {
                average = sum / count;
            }
            return(average);
        }
예제 #9
0
        private void LoadFromFile()
        {
            try
            {
                listBox1.Items.Clear();

                FileStream      f2 = new FileStream(fileName, FileMode.OpenOrCreate);
                BinaryFormatter bf = new BinaryFormatter();

                while (f2.Position < f2.Length)
                {
                    AverageTemp d = (AverageTemp)bf.Deserialize(f2); // восстановление объекта
                    if (!d.Deleted)
                    {
                        listBox1.Items.Add(d);
                    }
                }
                f2.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка при открытии файла", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #10
0
 public EditForm(AverageTemp averageTemp)
 {
     InitializeComponent();
     txtDayNumber.Text   = averageTemp.NumDay.ToString();
     txtTemperature.Text = averageTemp.Temperature.ToString();
 }