Простой способ записи данных - C#
Формулировка задачи:
Добрый день.
Ребят тут я задумался немного о том как лучше всего хранить историю записей.
INI
DATABASE
или еще какие варианты есть.
Я стремлюсь к тому чтобы был всего один EXE файл.
Вот какой способ посоветуете для записи данных.
Решение задачи: «Простой способ записи данных»
textual
Листинг программы
- using System;
- using System.Diagnostics;
- using System.IO;
- using RecentFilesExample.Data;
- using RecentFilesExample.Model;
- namespace RecentFilesExample
- {
- internal class Program
- {
- private void Run(string[] args)
- {
- string dataFolder = Path.Combine(
- Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "RecentFilesExample");
- FileDescription[] descriptions =
- {
- new FileDescription {FilePath = Path.Combine(dataFolder, "File1.txt")},
- new FileDescription {FilePath = Path.Combine(dataFolder, "File2.txt")},
- new FileDescription {FilePath = Path.Combine(dataFolder, "File3.txt")}
- };
- try
- {
- if (!Directory.Exists(dataFolder))
- {
- Directory.CreateDirectory(dataFolder);
- }
- string recentFilesFileName = Path.Combine(dataFolder, "recentFiles.xml");
- DataSerializer.SaveObject(descriptions, recentFilesFileName);
- Console.WriteLine("Данные сохранены.");
- Process.Start(dataFolder);
- FileDescription[] fromFile = DataSerializer.LoadObject<FileDescription[]>(recentFilesFileName);
- Console.WriteLine("Данные прочитаны из файла.");
- Write(fromFile);
- }
- catch (Exception ex)
- {
- HandleException(ex);
- }
- Console.WriteLine("Нажмите любую клавишу, чтобы завершить работу...");
- Console.ReadKey();
- try
- {
- if (Directory.Exists(dataFolder))
- {
- Directory.Delete(dataFolder, true);
- }
- }
- catch (Exception ex)
- {
- HandleException(ex, "Не удалось удалить папку: " + dataFolder);
- Console.ReadKey();
- }
- }
- private void HandleException(Exception ex, string message = null)
- {
- if (message != null)
- {
- Console.WriteLine(message);
- }
- Console.WriteLine(ex.Message);
- }
- private void Write(FileDescription[] descriptions)
- {
- Console.WriteLine("Recent files:");
- foreach (FileDescription description in descriptions)
- {
- Console.WriteLine(description.FilePath);
- }
- }
- #region Entry point
- private static Program _program;
- private static void Main(string[] args)
- {
- _program = new Program();
- _program.Run(args);
- }
- #endregion
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д