Move file locations path helpers in extensions so it can load these files.
This commit is contained in:
parent
f4de7d12ad
commit
976f839244
@ -21,7 +21,7 @@ namespace FileConverter.Diagnostics
|
|||||||
{
|
{
|
||||||
Debug.mainThreadId = Thread.CurrentThread.ManagedThreadId;
|
Debug.mainThreadId = Thread.CurrentThread.ManagedThreadId;
|
||||||
|
|
||||||
string path = PathHelpers.GetUserDataFolderPath();
|
string path = FileConverterExtension.PathHelpers.GetUserDataFolderPath;
|
||||||
|
|
||||||
// Delete old diagnostics folder (1 day).
|
// Delete old diagnostics folder (1 day).
|
||||||
DateTime expirationDate = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
|
DateTime expirationDate = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
|
||||||
|
@ -138,19 +138,6 @@ namespace FileConverter
|
|||||||
return true;
|
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)
|
public static string GenerateFilePathFromTemplate(string inputFilePath, OutputType outputFileExtension, string outputFilePathTemplate, int numberIndex, int numberMax)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(inputFilePath))
|
if (string.IsNullOrEmpty(inputFilePath))
|
||||||
|
@ -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))
|
public static T GetValue<T>(string key, T defaultValue = default(T))
|
||||||
{
|
{
|
||||||
Registry registry = Registry.Instance;
|
Registry registry = Registry.Instance;
|
||||||
@ -125,7 +135,7 @@ namespace FileConverter
|
|||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
// SAVE
|
// SAVE
|
||||||
string registryFilePath = Registry.GetUserRegistryFilePath();
|
string registryFilePath = Registry.GetUserRegistryFilePath;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -139,7 +149,7 @@ namespace FileConverter
|
|||||||
|
|
||||||
private static void Load()
|
private static void Load()
|
||||||
{
|
{
|
||||||
string registryFilePath = Registry.GetUserRegistryFilePath();
|
string registryFilePath = Registry.GetUserRegistryFilePath;
|
||||||
if (!File.Exists(registryFilePath))
|
if (!File.Exists(registryFilePath))
|
||||||
{
|
{
|
||||||
Registry.instance = new Registry();
|
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")]
|
[XmlRoot("Entry")]
|
||||||
public struct Entry
|
public struct Entry
|
||||||
{
|
{
|
||||||
|
@ -34,39 +34,12 @@ namespace FileConverter.Services
|
|||||||
get;
|
get;
|
||||||
private set;
|
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
|
private string UserSettingsTemporaryFilePath
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
string path = PathHelpers.GetUserDataFolderPath();
|
string path = FileConverterExtension.PathHelpers.GetUserDataFolderPath;
|
||||||
path = Path.Combine(path, "Settings.temp.xml");
|
path = Path.Combine(path, "Settings.temp.xml");
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
@ -79,11 +52,11 @@ namespace FileConverter.Services
|
|||||||
Settings defaultSettings = null;
|
Settings defaultSettings = null;
|
||||||
|
|
||||||
// Load the default settings.
|
// Load the default settings.
|
||||||
if (File.Exists(this.DefaultSettingsFilePath))
|
if (File.Exists(FileConverterExtension.PathHelpers.DefaultSettingsFilePath))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
XmlHelpers.LoadFromFile<Settings>("Settings", this.DefaultSettingsFilePath, out defaultSettings);
|
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.DefaultSettingsFilePath, out defaultSettings);
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@ -93,21 +66,21 @@ namespace FileConverter.Services
|
|||||||
}
|
}
|
||||||
else
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load user settings if exists.
|
// Load user settings if exists.
|
||||||
Settings userSettings = null;
|
Settings userSettings = null;
|
||||||
if (File.Exists(this.UserSettingsFilePath))
|
if (File.Exists(FileConverterExtension.PathHelpers.UserSettingsFilePath))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
XmlHelpers.LoadFromFile<Settings>("Settings", this.UserSettingsFilePath, out userSettings);
|
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.UserSettingsFilePath, out userSettings);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
File.Delete(this.UserSettingsFilePath);
|
File.Delete(FileConverterExtension.PathHelpers.UserSettingsFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userSettings != null)
|
if (userSettings != null)
|
||||||
@ -183,12 +156,12 @@ namespace FileConverter.Services
|
|||||||
private Settings Load()
|
private Settings Load()
|
||||||
{
|
{
|
||||||
Settings settings = null;
|
Settings settings = null;
|
||||||
if (File.Exists(this.UserSettingsFilePath))
|
if (File.Exists(FileConverterExtension.PathHelpers.UserSettingsFilePath))
|
||||||
{
|
{
|
||||||
Settings userSettings = null;
|
Settings userSettings = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
XmlHelpers.LoadFromFile<Settings>("Settings", this.UserSettingsFilePath, out userSettings);
|
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.UserSettingsFilePath, out userSettings);
|
||||||
settings = userSettings;
|
settings = userSettings;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
@ -201,7 +174,7 @@ namespace FileConverter.Services
|
|||||||
|
|
||||||
if (messageBoxResult == MessageBoxResult.Yes)
|
if (messageBoxResult == MessageBoxResult.Yes)
|
||||||
{
|
{
|
||||||
File.Delete(this.UserSettingsFilePath);
|
File.Delete(FileConverterExtension.PathHelpers.UserSettingsFilePath);
|
||||||
return this.Load();
|
return this.Load();
|
||||||
}
|
}
|
||||||
else if (messageBoxResult == MessageBoxResult.No)
|
else if (messageBoxResult == MessageBoxResult.No)
|
||||||
@ -222,12 +195,11 @@ namespace FileConverter.Services
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Load the default settings.
|
// Load the default settings.
|
||||||
if (File.Exists(this.DefaultSettingsFilePath))
|
if (File.Exists(FileConverterExtension.PathHelpers.DefaultSettingsFilePath))
|
||||||
{
|
{
|
||||||
Settings defaultSettings = null;
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
XmlHelpers.LoadFromFile<Settings>("Settings", this.DefaultSettingsFilePath, out defaultSettings);
|
XmlHelpers.LoadFromFile<Settings>("Settings", FileConverterExtension.PathHelpers.DefaultSettingsFilePath, out Settings defaultSettings);
|
||||||
settings = defaultSettings;
|
settings = defaultSettings;
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
@ -237,7 +209,7 @@ namespace FileConverter.Services
|
|||||||
}
|
}
|
||||||
else
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,38 +12,7 @@ namespace FileConverter
|
|||||||
{
|
{
|
||||||
public static void LoadFromFile<T>(string root, string path, out T deserializedObject)
|
public static void LoadFromFile<T>(string root, string path, out T deserializedObject)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(path))
|
FileConverterExtension.XmlHelpers.LoadFromFile(root, path, out deserializedObject);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (deserializedObject is IXmlSerializable xmlSerializableObject)
|
if (deserializedObject is IXmlSerializable xmlSerializableObject)
|
||||||
{
|
{
|
||||||
@ -53,38 +22,9 @@ namespace FileConverter
|
|||||||
|
|
||||||
public static void SaveToFile<T>(string root, string path, T objectToSerialize)
|
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
|
try
|
||||||
{
|
{
|
||||||
using (StreamWriter writer = new StreamWriter(path))
|
FileConverterExtension.XmlHelpers.SaveToFile(root, path, objectToSerialize);
|
||||||
{
|
|
||||||
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
|
|
||||||
{
|
|
||||||
Indent = true,
|
|
||||||
IndentChars = " "
|
|
||||||
};
|
|
||||||
|
|
||||||
using (XmlWriter xmlWriter = XmlWriter.Create(writer, xmlWriterSettings))
|
|
||||||
{
|
|
||||||
serializer.Serialize(xmlWriter, objectToSerialize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (System.Exception exception)
|
catch (System.Exception exception)
|
||||||
{
|
{
|
||||||
|
76
Application/FileConverterExtension/PathHelpers.cs
Normal file
76
Application/FileConverterExtension/PathHelpers.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
75
Application/FileConverterExtension/XmlHelpers.cs
Normal file
75
Application/FileConverterExtension/XmlHelpers.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user