using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarManagerV3 { internal class SafeManager { private static readonly string recentPathsFile = "recent_paths.txt"; public static void InitializeFile(string path) { if (!File.Exists(@path)) { using (StreamWriter writer = new StreamWriter(@path)) { // Create the file, empty writer.WriteLine(); } } } public static List ReadCars(string path) { List cars = new List(); using (StreamReader reader = new StreamReader(@path)) { string line; while ((line = reader.ReadLine()) != null) { // Process the line if (line == "") continue; cars.Add(Car.FromCsvString(line)); } } return cars; } public static void SaveCars(string path, List cars) { using (StreamWriter writer = new StreamWriter(@path)) { foreach (Car car in cars) { writer.WriteLine(car.ToCsvString()); } } } public static void AddRecentPath(string path) { // Read the file, if the path is not already in the file, add it to the top. // If it is already in the file, move it to the top. // Keep only the 5 most recent paths. List paths = new List(); if (File.Exists(recentPathsFile)) { using (StreamReader reader = new StreamReader(recentPathsFile)) { string line; while ((line = reader.ReadLine()) != null) { paths.Add(line); } } } paths.Remove(path); paths.Insert(0, path); if (paths.Count > 5) { paths = paths.Take(5).ToList(); } using (StreamWriter writer = new StreamWriter(recentPathsFile)) { foreach (string p in paths) { writer.WriteLine(p); } } } public static List GetRecentPaths() { List paths = new List(); if (File.Exists(recentPathsFile)) { using (StreamReader reader = new StreamReader(recentPathsFile)) { string line; while ((line = reader.ReadLine()) != null) { paths.Add(line); } } } return paths; } } }