Skip to content

Commit

Permalink
Fix error on startup with broken XML config
Browse files Browse the repository at this point in the history
Fixes SevenbytesSoftware#22

Fix error on startup with broken XML config.

* Add a try-catch block around the deserialization code in the `LoadConfig` method to handle broken XML config files.
* Show an error message if an exception occurs during deserialization and create a new default `Settings` object.
Likhithsai2580 committed Dec 31, 2024
1 parent 9a965d2 commit b0b075e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions SevenConverter/Actions/Config.cs
Original file line number Diff line number Diff line change
@@ -26,10 +26,18 @@ private void LoadConfig()
config_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Files.Config_Folder);
if (File.Exists(Path.Combine(config_path, Files.Config_Filename)))
{
using (FileStream file = new FileStream(Path.Combine(config_path, Files.Config_Filename), FileMode.Open))
try
{
XmlSerializer xml = new XmlSerializer(typeof(Settings));
settings = xml.Deserialize(file) as Settings;
using (FileStream file = new FileStream(Path.Combine(config_path, Files.Config_Filename), FileMode.Open))
{
XmlSerializer xml = new XmlSerializer(typeof(Settings));
settings = xml.Deserialize(file) as Settings;
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading config file: {ex.Message}. A new default config will be created.");
settings = new Settings();
}
}
else
@@ -58,4 +66,4 @@ private void SaveConfig()

#endregion Private Methods
}
}
}

0 comments on commit b0b075e

Please sign in to comment.