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

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -18,10 +19,14 @@ namespace CarManagerV3
public static void InitializeImageFolder() public static void InitializeImageFolder()
{ {
string path = "images"; string path = "images";
if (!System.IO.Directory.Exists(path)) if (!System.IO.Directory.Exists(path))
{ {
System.IO.Directory.CreateDirectory(path); System.IO.Directory.CreateDirectory(path);
} }
// Do not catch here. If we cannot create our images folder, the program wont work.
} }
/// <summary> /// <summary>
@@ -47,15 +52,18 @@ namespace CarManagerV3
FetchImage(car); FetchImage(car);
string path = GetImagePath(car); string path = GetImagePath(car);
// does image exist? // does image exist?
try
{
if (System.IO.File.Exists(path)) if (System.IO.File.Exists(path))
{ {
return Image.FromFile(path); return Image.FromFile(path);
} }
else
{
return Image.FromFile("images/no_image_available.png");
} }
catch (Exception ex)
{
Console.Error.WriteLine($"Error loading image: {ex.Message}");
}
return Image.FromFile("images/no_image_available.png");
} }
/// <summary> /// <summary>

View File

@@ -27,6 +27,7 @@ namespace CarManagerV3
} }
SafeManager.InitializeFile(filepath); SafeManager.InitializeFile(filepath);
StateManager.setFilePath(filepath);
List<Car> _cars = SafeManager.ReadCars(filepath); List<Car> _cars = SafeManager.ReadCars(filepath);
refreshCars(_cars); refreshCars(_cars);

View File

@@ -23,6 +23,8 @@ namespace CarManagerV3
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
public static void InitializeFile(string path) public static void InitializeFile(string path)
{
try
{ {
if (!File.Exists(@path)) if (!File.Exists(@path))
{ {
@@ -30,9 +32,15 @@ namespace CarManagerV3
{ {
// Create the file, empty // Create the file, empty
writer.WriteLine(); writer.WriteLine();
writer.Close();
} }
} }
} }
catch (Exception ex)
{
Console.Error.WriteLine($"Error initializing file: {ex.Message}");
}
}
/// <summary> /// <summary>
/// Reads cars from a specified file path. /// Reads cars from a specified file path.
@@ -44,6 +52,8 @@ namespace CarManagerV3
public static List<Car> ReadCars(string path) public static List<Car> ReadCars(string path)
{ {
List<Car> cars = new List<Car>(); List<Car> cars = new List<Car>();
try
{
using (StreamReader reader = new StreamReader(@path)) using (StreamReader reader = new StreamReader(@path))
{ {
string line; string line;
@@ -53,6 +63,11 @@ namespace CarManagerV3
if (line == "") continue; if (line == "") continue;
cars.Add(Car.FromCsvString(line)); cars.Add(Car.FromCsvString(line));
} }
reader.Close();
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error reading cars from file: {ex.Message}");
} }
return cars; return cars;
} }
@@ -63,6 +78,8 @@ namespace CarManagerV3
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
/// <param name="cars">A <see cref="List{Car}"/> containing all cars to save.</param> /// <param name="cars">A <see cref="List{Car}"/> containing all cars to save.</param>
public static void SaveCars(string path, List<Car> cars) public static void SaveCars(string path, List<Car> cars)
{
try
{ {
using (StreamWriter writer = new StreamWriter(@path)) using (StreamWriter writer = new StreamWriter(@path))
{ {
@@ -70,6 +87,11 @@ namespace CarManagerV3
{ {
writer.WriteLine(car.ToCsvString()); writer.WriteLine(car.ToCsvString());
} }
writer.Close();
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error saving cars to file: {ex.Message}");
} }
} }
@@ -83,6 +105,8 @@ namespace CarManagerV3
public static void AddRecentPath(string path) public static void AddRecentPath(string path)
{ {
List<string> paths = new List<string>(); List<string> paths = new List<string>();
try
{
if (File.Exists(recentPathsFile)) if (File.Exists(recentPathsFile))
{ {
using (StreamReader reader = new StreamReader(recentPathsFile)) using (StreamReader reader = new StreamReader(recentPathsFile))
@@ -92,6 +116,7 @@ namespace CarManagerV3
{ {
paths.Add(line); paths.Add(line);
} }
reader.Close();
} }
} }
paths.Remove(path); paths.Remove(path);
@@ -106,6 +131,11 @@ namespace CarManagerV3
{ {
writer.WriteLine(p); writer.WriteLine(p);
} }
writer.Close();
}
} catch (Exception ex)
{
Console.Error.WriteLine($"Error managing recent paths: {ex.Message}");
} }
} }
@@ -118,6 +148,8 @@ namespace CarManagerV3
public static List<string> GetRecentPaths() public static List<string> GetRecentPaths()
{ {
List<string> paths = new List<string>(); List<string> paths = new List<string>();
try
{
if (File.Exists(recentPathsFile)) if (File.Exists(recentPathsFile))
{ {
using (StreamReader reader = new StreamReader(recentPathsFile)) using (StreamReader reader = new StreamReader(recentPathsFile))
@@ -127,8 +159,13 @@ namespace CarManagerV3
{ {
paths.Add(line); paths.Add(line);
} }
reader.Close();
} }
} }
} catch (Exception ex)
{
Console.Error.WriteLine($"Error reading recent paths: {ex.Message}");
}
return paths; return paths;
} }
} }