public static Config CreateOrLoadByPath(string fullPath) {
string configPath = Path.GetDirectoryName(fullPath);
Config config;
// Create configuration folder if it does not exist yet.
if (!Directory.Exists(configPath)) {
Directory.CreateDirectory(configPath);
}
// Create an empty XML configuration file if none is present yet.
if (!File.Exists(fullPath)) {
Config conf = CreateInitialConfig(fullPath);
conf.Save();
}
// Load the XML configuration.
try {
config = Load(fullPath);
} catch (TypeInitializationException) {
config = CreateInitialConfig(fullPath);
} catch (FileNotFoundException) {
config = CreateInitialConfig(fullPath);
} catch (XmlException) {
FileInfo file = new FileInfo(fullPath);
// If the XML configuration file exists but with file size zero, then recreate it.
if (file.Length == 0) {
File.Delete(fullPath);
config = CreateInitialConfig(fullPath);
} else {
throw new XmlException(fullPath + " does not contain a valid config XML structure.");
}
} finally {
config = Load(fullPath);
}
return config;
}