fix: safeManager error handeling & Load fix

This commit is contained in:
2026-02-19 08:25:07 +01:00
parent e431a05124
commit 96d9334b56
3 changed files with 85 additions and 39 deletions

View File

@@ -24,14 +24,22 @@ namespace CarManagerV3
/// <param name="path">The path.</param>
public static void InitializeFile(string path)
{
if (!File.Exists(@path))
try
{
using (StreamWriter writer = new StreamWriter(@path))
if (!File.Exists(@path))
{
// Create the file, empty
writer.WriteLine();
using (StreamWriter writer = new StreamWriter(@path))
{
// Create the file, empty
writer.WriteLine();
writer.Close();
}
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error initializing file: {ex.Message}");
}
}
/// <summary>
@@ -44,15 +52,22 @@ namespace CarManagerV3
public static List<Car> ReadCars(string path)
{
List<Car> cars = new List<Car>();
using (StreamReader reader = new StreamReader(@path))
try
{
string line;
while ((line = reader.ReadLine()) != null)
using (StreamReader reader = new StreamReader(@path))
{
// Process the line
if (line == "") continue;
cars.Add(Car.FromCsvString(line));
string line;
while ((line = reader.ReadLine()) != null)
{
// Process the line
if (line == "") continue;
cars.Add(Car.FromCsvString(line));
}
reader.Close();
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error reading cars from file: {ex.Message}");
}
return cars;
}
@@ -64,12 +79,19 @@ namespace CarManagerV3
/// <param name="cars">A <see cref="List{Car}"/> containing all cars to save.</param>
public static void SaveCars(string path, List<Car> cars)
{
using (StreamWriter writer = new StreamWriter(@path))
try
{
foreach (Car car in cars)
using (StreamWriter writer = new StreamWriter(@path))
{
writer.WriteLine(car.ToCsvString());
foreach (Car car in cars)
{
writer.WriteLine(car.ToCsvString());
}
writer.Close();
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error saving cars to file: {ex.Message}");
}
}
@@ -83,29 +105,37 @@ namespace CarManagerV3
public static void AddRecentPath(string path)
{
List<string> paths = new List<string>();
if (File.Exists(recentPathsFile))
try
{
using (StreamReader reader = new StreamReader(recentPathsFile))
if (File.Exists(recentPathsFile))
{
string line;
while ((line = reader.ReadLine()) != null)
using (StreamReader reader = new StreamReader(recentPathsFile))
{
paths.Add(line);
string line;
while ((line = reader.ReadLine()) != null)
{
paths.Add(line);
}
reader.Close();
}
}
}
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)
paths.Remove(path);
paths.Insert(0, path);
if (paths.Count > 5)
{
writer.WriteLine(p);
paths = paths.Take(5).ToList();
}
using (StreamWriter writer = new StreamWriter(recentPathsFile))
{
foreach (string p in paths)
{
writer.WriteLine(p);
}
writer.Close();
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error managing recent paths: {ex.Message}");
}
}
@@ -118,16 +148,23 @@ namespace CarManagerV3
public static List<string> GetRecentPaths()
{
List<string> paths = new List<string>();
if (File.Exists(recentPathsFile))
try
{
using (StreamReader reader = new StreamReader(recentPathsFile))
if (File.Exists(recentPathsFile))
{
string line;
while ((line = reader.ReadLine()) != null)
using (StreamReader reader = new StreamReader(recentPathsFile))
{
paths.Add(line);
string line;
while ((line = reader.ReadLine()) != null)
{
paths.Add(line);
}
reader.Close();
}
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error reading recent paths: {ex.Message}");
}
return paths;
}