diff --git a/Application/FileConverter/Diagnostics/Debug.cs b/Application/FileConverter/Diagnostics/Debug.cs index 603f636..f720411 100644 --- a/Application/FileConverter/Diagnostics/Debug.cs +++ b/Application/FileConverter/Diagnostics/Debug.cs @@ -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)); diff --git a/Application/FileConverter/PathHelpers.cs b/Application/FileConverter/PathHelpers.cs index 6d49641..9c2d6ed 100644 --- a/Application/FileConverter/PathHelpers.cs +++ b/Application/FileConverter/PathHelpers.cs @@ -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)) diff --git a/Application/FileConverter/Registry.cs b/Application/FileConverter/Registry.cs index a9b7e03..477248c 100644 --- a/Application/FileConverter/Registry.cs +++ b/Application/FileConverter/Registry.cs @@ -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(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 { diff --git a/Application/FileConverter/Services/SettingsService.cs b/Application/FileConverter/Services/SettingsService.cs index 99f18fb..7e5b8c1 100644 --- a/Application/FileConverter/Services/SettingsService.cs +++ b/Application/FileConverter/Services/SettingsService.cs @@ -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", this.DefaultSettingsFilePath, out defaultSettings); + XmlHelpers.LoadFromFile("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", this.UserSettingsFilePath, out userSettings); + XmlHelpers.LoadFromFile("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", this.UserSettingsFilePath, out userSettings); + XmlHelpers.LoadFromFile("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", this.DefaultSettingsFilePath, out defaultSettings); + XmlHelpers.LoadFromFile("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); } } diff --git a/Application/FileConverter/XmlHelpers.cs b/Application/FileConverter/XmlHelpers.cs index 1fdb75b..e06e79f 100644 --- a/Application/FileConverter/XmlHelpers.cs +++ b/Application/FileConverter/XmlHelpers.cs @@ -12,38 +12,7 @@ namespace FileConverter { public static void LoadFromFile(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(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) { diff --git a/Application/FileConverterExtension/PathHelpers.cs b/Application/FileConverterExtension/PathHelpers.cs new file mode 100644 index 0000000..21d66f2 --- /dev/null +++ b/Application/FileConverterExtension/PathHelpers.cs @@ -0,0 +1,76 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +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; + } + } + } +} diff --git a/Application/FileConverterExtension/XmlHelpers.cs b/Application/FileConverterExtension/XmlHelpers.cs new file mode 100644 index 0000000..fe18afd --- /dev/null +++ b/Application/FileConverterExtension/XmlHelpers.cs @@ -0,0 +1,75 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverterExtension +{ + using System; + using System.IO; + using System.Xml; + using System.Xml.Serialization; + + public class XmlHelpers + { + public static void LoadFromFile(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(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); + } + } + } + } +} \ No newline at end of file