104 lines
3.0 KiB
C#
104 lines
3.0 KiB
C#
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<Car> ReadCars(string path)
|
|
{
|
|
List<Car> cars = new List<Car>();
|
|
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<Car> 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<string> paths = new List<string>();
|
|
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<string> GetRecentPaths()
|
|
{
|
|
List<string> paths = new List<string>();
|
|
if (File.Exists(recentPathsFile))
|
|
{
|
|
using (StreamReader reader = new StreamReader(recentPathsFile))
|
|
{
|
|
string line;
|
|
while ((line = reader.ReadLine()) != null)
|
|
{
|
|
paths.Add(line);
|
|
}
|
|
}
|
|
}
|
|
return paths;
|
|
}
|
|
}
|
|
}
|