Advertisement
KAYOver

Write

Jun 18th, 2021
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. private void writeFile()
  2. {
  3.     System.IO.FileInfo fi;
  4.     System.IO.StreamWriter sw;
  5.     System.IO.StreamReader sr;
  6.     List<string> lines = new List<string>();
  7.  
  8.     try
  9.     {
  10.         fi = new System.IO.FileInfo(textBox3.Text);
  11.     }
  12.     catch
  13.     {
  14.         MessageBox.Show("Путь имеет неверный формат.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  15.         return;
  16.     }
  17.  
  18.     if(!textBox3.Text.StartsWith(Application.StartupPath) &&
  19.         MessageBox.Show("Файл не находится в папке приложения, продолжить?", "Предупреждение", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
  20.     {
  21.         return;
  22.     }
  23.  
  24.     if(fi.Exists)
  25.     {
  26.         sr = new System.IO.StreamReader(textBox3.Text, Encoding.GetEncoding(1251));
  27.         lines = sr.ReadToEnd().Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
  28.         sr.Close();
  29.     }
  30.  
  31.     sw = fi.CreateText();
  32.     double temp = Convert.ToDouble(textBox1.Text);
  33.     DateTime date = monthCalendar1.SelectionStart;
  34.  
  35.     while(monthCalendar1.SelectionEnd >= date)
  36.     {
  37.         int cloneIndex = lines.FindIndex(v => v.StartsWith(date.ToString("d")));
  38.  
  39.         if(cloneIndex > -1)
  40.         {
  41.             if(MessageBox.Show("Запись на дату " + date.ToString("d") + " уже существует.\nЗаменить?", "Совданение", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  42.             {
  43.                 lines.RemoveAt(cloneIndex);
  44.             }
  45.             else
  46.             {
  47.                 continue;
  48.             }
  49.         }
  50.  
  51.         lines.Add(date.ToString("d")+" "+temp.ToString("N"));
  52.         date = date.AddDays(1);
  53.     }
  54.  
  55.     foreach(string line in lines)
  56.     {
  57.         sw.WriteLine(line);
  58.     }
  59.     sw.Close();
  60.     textBox1.Text = "";
  61.     readFile();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement