Простой способ записи данных - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день. Ребят тут я задумался немного о том как лучше всего хранить историю записей. INI DATABASE или еще какие варианты есть. Я стремлюсь к тому чтобы был всего один EXE файл. Вот какой способ посоветуете для записи данных.

Решение задачи: «Простой способ записи данных»

textual
Листинг программы
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using RecentFilesExample.Data;
  5. using RecentFilesExample.Model;
  6.  
  7. namespace RecentFilesExample
  8. {
  9.     internal class Program
  10.     {
  11.         private void Run(string[] args)
  12.         {
  13.             string dataFolder = Path.Combine(
  14.                 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "RecentFilesExample");
  15.  
  16.             FileDescription[] descriptions =
  17.             {
  18.                 new FileDescription {FilePath = Path.Combine(dataFolder, "File1.txt")},
  19.                 new FileDescription {FilePath = Path.Combine(dataFolder, "File2.txt")},
  20.                 new FileDescription {FilePath = Path.Combine(dataFolder, "File3.txt")}
  21.             };
  22.  
  23.             try
  24.             {
  25.                 if (!Directory.Exists(dataFolder))
  26.                 {
  27.                     Directory.CreateDirectory(dataFolder);
  28.                 }
  29.  
  30.                 string recentFilesFileName = Path.Combine(dataFolder, "recentFiles.xml");
  31.  
  32.                 DataSerializer.SaveObject(descriptions, recentFilesFileName);
  33.                 Console.WriteLine("Данные сохранены.");
  34.                 Process.Start(dataFolder);
  35.  
  36.                 FileDescription[] fromFile = DataSerializer.LoadObject<FileDescription[]>(recentFilesFileName);
  37.                 Console.WriteLine("Данные прочитаны из файла.");
  38.  
  39.                 Write(fromFile);
  40.             }
  41.             catch (Exception ex)
  42.             {
  43.                 HandleException(ex);
  44.             }
  45.  
  46.             Console.WriteLine("Нажмите любую клавишу, чтобы завершить работу...");
  47.             Console.ReadKey();
  48.  
  49.             try
  50.             {
  51.                 if (Directory.Exists(dataFolder))
  52.                 {
  53.                     Directory.Delete(dataFolder, true);
  54.                 }
  55.             }
  56.             catch (Exception ex)
  57.             {
  58.                 HandleException(ex, "Не удалось удалить папку: " + dataFolder);
  59.                 Console.ReadKey();
  60.             }
  61.         }
  62.  
  63.         private void HandleException(Exception ex, string message = null)
  64.         {
  65.             if (message != null)
  66.             {
  67.                 Console.WriteLine(message);
  68.             }
  69.             Console.WriteLine(ex.Message);
  70.         }
  71.  
  72.         private void Write(FileDescription[] descriptions)
  73.         {
  74.             Console.WriteLine("Recent files:");
  75.             foreach (FileDescription description in descriptions)
  76.             {
  77.                 Console.WriteLine(description.FilePath);
  78.             }
  79.         }
  80.  
  81.         #region Entry point
  82.  
  83.         private static Program _program;
  84.  
  85.         private static void Main(string[] args)
  86.         {
  87.             _program = new Program();
  88.             _program.Run(args);
  89.         }
  90.  
  91.         #endregion
  92.     }
  93. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.8 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы