Move file locations path helpers in extensions so it can load these files.

This commit is contained in:
Adrien Allard 2021-03-10 09:07:35 +01:00
parent f4de7d12ad
commit 976f839244
7 changed files with 180 additions and 127 deletions

View File

@ -21,7 +21,7 @@ namespace FileConverter.Diagnostics
{
Debug.mainThreadId = Thread.CurrentThread.ManagedThreadId;
string path = PathHelpers.GetUserDataFolderPath();
string path = FileConverterExtension.PathHelpers.GetUserDataFolderPath;
// Delete old diagnostics folder (1 day).
DateTime expirationDate = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));

View File

@ -138,19 +138,6 @@ namespace FileConverter
return true;
}
public static string GetUserDataFolderPath()
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
path = System.IO.Path.Combine(path, "FileConverter");
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
return path;
}
public static string GenerateFilePathFromTemplate(string inputFilePath, OutputType outputFileExtension, string outputFilePathTemplate, int numberIndex, int numberMax)
{
if (string.IsNullOrEmpty(inputFilePath))

View File

@ -80,6 +80,16 @@ namespace FileConverter
}
}
private static string GetUserRegistryFilePath
{
get
{
string path = FileConverterExtension.PathHelpers.GetUserDataFolderPath;
path = Path.Combine(path, "Registry.xml");
return path;
}
}
public static T GetValue<T>(string key, T defaultValue = default(T))
{
Registry registry = Registry.Instance;
@ -125,7 +135,7 @@ namespace FileConverter
public void Dispose()
{
// SAVE
string registryFilePath = Registry.GetUserRegistryFilePath();
string registryFilePath = Registry.GetUserRegistryFilePath;
try
{
@ -139,7 +149,7 @@ namespace FileConverter
private static void Load()
{
string registryFilePath = Registry.GetUserRegistryFilePath();
string registryFilePath = Registry.GetUserRegistryFilePath;
if (!File.Exists(registryFilePath))
{
Registry.instance = new Registry();
@ -163,13 +173,6 @@ namespace FileConverter
}
}
private static string GetUserRegistryFilePath()
{
string path = PathHelpers.GetUserDataFolderPath();
path = Path.Combine(path, "Registry.xml");
return path;
}
[XmlRoot("Entry")]
public struct Entry
{

View File

@ -34,39 +34,12 @@ namespace FileConverter.Services
get;
private set;
}
private string DefaultSettingsFilePath
{
get
{
string path = Assembly.GetEntryAssembly().Location;
path = Path.GetDirectoryName(path);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = Path.Combine(path, "Settings.default.xml");
return path;
}
}
private string UserSettingsFilePath
{
get
{
string path = PathHelpers.GetUserDataFolderPath();
path = Path.Combine(path, "Settings.user.xml");
return path;
}
}
private string UserSettingsTemporaryFilePath
{
get
{
string path = PathHelpers.GetUserDataFolderPath();
string path = FileConverterExtension.PathHelpers.GetUserDataFolderPath;
path = Path.Combine(path, "Settings.temp.xml");
return path;
}
@ -79,11 +52,11 @@ namespace FileConverter.Services
Settings defaultSettings = null;
// Load the default settings.
if (File.Exists(this.DefaultSettingsFilePath))
if (File.Exists(FileConverterExtension.PathHelpers.DefaultSettingsFilePath))
{
try
{
XmlHelpers.LoadFromFile<Settings>("Settings", this.DefaultSettingsFilePath, out defaultSettings);
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.DefaultSettingsFilePath, out defaultSettings);
}
catch (Exception exception)
{
@ -93,21 +66,21 @@ namespace FileConverter.Services
}
else
{
Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", this.DefaultSettingsFilePath);
Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", FileConverterExtension.PathHelpers.DefaultSettingsFilePath);
return false;
}
// Load user settings if exists.
Settings userSettings = null;
if (File.Exists(this.UserSettingsFilePath))
if (File.Exists(FileConverterExtension.PathHelpers.UserSettingsFilePath))
{
try
{
XmlHelpers.LoadFromFile<Settings>("Settings", this.UserSettingsFilePath, out userSettings);
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.UserSettingsFilePath, out userSettings);
}
catch (Exception)
{
File.Delete(this.UserSettingsFilePath);
File.Delete(FileConverterExtension.PathHelpers.UserSettingsFilePath);
}
if (userSettings != null)
@ -183,12 +156,12 @@ namespace FileConverter.Services
private Settings Load()
{
Settings settings = null;
if (File.Exists(this.UserSettingsFilePath))
if (File.Exists(FileConverterExtension.PathHelpers.UserSettingsFilePath))
{
Settings userSettings = null;
try
{
XmlHelpers.LoadFromFile<Settings>("Settings", this.UserSettingsFilePath, out userSettings);
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.UserSettingsFilePath, out userSettings);
settings = userSettings;
}
catch (Exception)
@ -201,7 +174,7 @@ namespace FileConverter.Services
if (messageBoxResult == MessageBoxResult.Yes)
{
File.Delete(this.UserSettingsFilePath);
File.Delete(FileConverterExtension.PathHelpers.UserSettingsFilePath);
return this.Load();
}
else if (messageBoxResult == MessageBoxResult.No)
@ -222,12 +195,11 @@ namespace FileConverter.Services
else
{
// Load the default settings.
if (File.Exists(this.DefaultSettingsFilePath))
if (File.Exists(FileConverterExtension.PathHelpers.DefaultSettingsFilePath))
{
Settings defaultSettings = null;
try
{
XmlHelpers.LoadFromFile<Settings>("Settings", this.DefaultSettingsFilePath, out defaultSettings);
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.DefaultSettingsFilePath, out Settings defaultSettings);
settings = defaultSettings;
}
catch (Exception exception)
@ -237,7 +209,7 @@ namespace FileConverter.Services
}
else
{
Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", this.DefaultSettingsFilePath);
Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", FileConverterExtension.PathHelpers.DefaultSettingsFilePath);
}
}

View File

@ -12,38 +12,7 @@ namespace FileConverter
{
public static void LoadFromFile<T>(string root, string path, out T deserializedObject)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
XmlRootAttribute xmlRoot = new XmlRootAttribute
{
ElementName = root
};
XmlSerializer serializer = new XmlSerializer(typeof(T), xmlRoot);
try
{
using (StreamReader reader = new StreamReader(path))
{
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings
{
IgnoreWhitespace = true,
IgnoreComments = true
};
using (XmlReader xmlReader = XmlReader.Create(reader, xmlReaderSettings))
{
deserializedObject = (T)serializer.Deserialize(xmlReader);
}
}
}
catch (System.Exception)
{
throw;
}
FileConverterExtension.XmlHelpers.LoadFromFile(root, path, out deserializedObject);
if (deserializedObject is IXmlSerializable xmlSerializableObject)
{
@ -53,38 +22,9 @@ namespace FileConverter
public static void SaveToFile<T>(string root, string path, T objectToSerialize)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
if (objectToSerialize == null)
{
throw new ArgumentNullException(nameof(objectToSerialize));
}
XmlRootAttribute xmlRoot = new XmlRootAttribute
{
ElementName = root
};
XmlSerializer serializer = new XmlSerializer(typeof(T), xmlRoot);
try
{
using (StreamWriter writer = new StreamWriter(path))
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
Indent = true,
IndentChars = " "
};
using (XmlWriter xmlWriter = XmlWriter.Create(writer, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, objectToSerialize);
}
}
FileConverterExtension.XmlHelpers.SaveToFile(root, path, objectToSerialize);
}
catch (System.Exception exception)
{

View File

@ -0,0 +1,76 @@
// <copyright file="FileConverterExtension.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
namespace FileConverterExtension
{
using System.IO;
using System;
using Microsoft.Win32;
public static class PathHelpers
{
private static RegistryKey fileConverterRegistryKey;
private static string fileConverterPath;
public static string UserSettingsFilePath => Path.Combine(PathHelpers.GetUserDataFolderPath, "Settings.user.xml");
public static string DefaultSettingsFilePath
{
get
{
string pathToFileConverterExecutable = PathHelpers.FileConverterPath;
if (string.IsNullOrEmpty(pathToFileConverterExecutable))
{
return null;
}
return Path.Combine(Path.GetDirectoryName(pathToFileConverterExecutable), "Settings.default.xml");
}
}
public static RegistryKey FileConverterRegistryKey
{
get
{
if (PathHelpers.fileConverterRegistryKey == null)
{
PathHelpers.fileConverterRegistryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter");
if (PathHelpers.fileConverterRegistryKey == null)
{
throw new Exception("Can't retrieve file converter registry entry.");
}
}
return PathHelpers.fileConverterRegistryKey;
}
}
public static string FileConverterPath
{
get
{
if (string.IsNullOrEmpty(PathHelpers.fileConverterPath))
{
PathHelpers.fileConverterPath = PathHelpers.FileConverterRegistryKey.GetValue("Path") as string;
}
return PathHelpers.fileConverterPath;
}
}
public static string GetUserDataFolderPath
{
get
{
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
path = Path.Combine(path, "FileConverter");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
}
}
}

View File

@ -0,0 +1,75 @@
// <copyright file="XmlHelpers.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
namespace FileConverterExtension
{
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class XmlHelpers
{
public static void LoadFromFile<T>(string root, string path, out T deserializedObject)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
XmlRootAttribute xmlRoot = new XmlRootAttribute
{
ElementName = root
};
XmlSerializer serializer = new XmlSerializer(typeof(T), xmlRoot);
using (StreamReader reader = new StreamReader(path))
{
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings
{
IgnoreWhitespace = true,
IgnoreComments = true
};
using (XmlReader xmlReader = XmlReader.Create(reader, xmlReaderSettings))
{
deserializedObject = (T)serializer.Deserialize(xmlReader);
}
}
}
public static void SaveToFile<T>(string root, string path, T objectToSerialize)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
if (objectToSerialize == null)
{
throw new ArgumentNullException(nameof(objectToSerialize));
}
XmlRootAttribute xmlRoot = new XmlRootAttribute
{
ElementName = root
};
XmlSerializer serializer = new XmlSerializer(typeof(T), xmlRoot);
using (StreamWriter writer = new StreamWriter(path))
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
Indent = true,
IndentChars = " "
};
using (XmlWriter xmlWriter = XmlWriter.Create(writer, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, objectToSerialize);
}
}
}
}
}