diff --git a/CarManagerV3/ImageManager.cs b/CarManagerV3/ImageManager.cs index ae305d7..d811396 100644 --- a/CarManagerV3/ImageManager.cs +++ b/CarManagerV3/ImageManager.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; @@ -18,10 +19,14 @@ namespace CarManagerV3 public static void InitializeImageFolder() { string path = "images"; + if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } + + // Do not catch here. If we cannot create our images folder, the program wont work. + } /// @@ -47,15 +52,18 @@ namespace CarManagerV3 FetchImage(car); string path = GetImagePath(car); // does image exist? - if (System.IO.File.Exists(path)) + try { - return Image.FromFile(path); + if (System.IO.File.Exists(path)) + { + return Image.FromFile(path); + } } - else + catch (Exception ex) { - return Image.FromFile("images/no_image_available.png"); + Console.Error.WriteLine($"Error loading image: {ex.Message}"); } - + return Image.FromFile("images/no_image_available.png"); } /// diff --git a/CarManagerV3/MainForm.cs b/CarManagerV3/MainForm.cs index 4592b55..d8d8510 100644 --- a/CarManagerV3/MainForm.cs +++ b/CarManagerV3/MainForm.cs @@ -27,6 +27,7 @@ namespace CarManagerV3 } SafeManager.InitializeFile(filepath); + StateManager.setFilePath(filepath); List _cars = SafeManager.ReadCars(filepath); refreshCars(_cars); diff --git a/CarManagerV3/SafeManager.cs b/CarManagerV3/SafeManager.cs index 4cc396e..10e4a58 100644 --- a/CarManagerV3/SafeManager.cs +++ b/CarManagerV3/SafeManager.cs @@ -24,14 +24,22 @@ namespace CarManagerV3 /// The path. 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}"); + } } /// @@ -44,15 +52,22 @@ namespace CarManagerV3 public static List ReadCars(string path) { List cars = new List(); - 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 /// A containing all cars to save. public static void SaveCars(string path, List 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 paths = new List(); - 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 GetRecentPaths() { List paths = new List(); - 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; }