121 lines
3.4 KiB
C#
121 lines
3.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
|
|
namespace MicronSync
|
|
{
|
|
/// <summary>
|
|
/// Settings to store.
|
|
/// </summary>
|
|
public class Values
|
|
{
|
|
[SaveToConfig]
|
|
public Version MSVersion
|
|
{
|
|
get
|
|
{
|
|
return Assembly.GetExecutingAssembly().GetName().Version;
|
|
}
|
|
}
|
|
|
|
[SaveToConfig]
|
|
public string Description { get; set; }
|
|
|
|
[SaveToConfig]
|
|
public string BackupSource { get; set; }
|
|
|
|
[SaveToConfig]
|
|
public string BackupDestination { get; set; }
|
|
|
|
[SaveToConfig]
|
|
public string RestoreSource { get; set; }
|
|
|
|
[SaveToConfig]
|
|
public string RestoreDestination { get; set; }
|
|
|
|
[SaveToConfig]
|
|
public bool EnablePurge { get; set; }
|
|
|
|
[SaveToConfig]
|
|
public bool EnableBackup { get; set; }
|
|
|
|
[SaveToConfig]
|
|
public int CompressionLevel { get; set; } = 4;
|
|
|
|
public string openFile { get; set; }
|
|
|
|
/// <summary>
|
|
/// Flags value to be included in configuration.
|
|
/// </summary>
|
|
internal class SaveToConfigAttribute : Attribute{}
|
|
}
|
|
|
|
public class MSConfig : Values
|
|
{
|
|
public int Save()
|
|
{
|
|
int errors = 0;
|
|
try
|
|
{
|
|
// Delete existing config file.
|
|
if (File.Exists(openFile))
|
|
File.Delete(openFile);
|
|
|
|
StreamWriter writer = new StreamWriter(openFile, true);
|
|
|
|
foreach (var item in GetType().GetProperties())
|
|
{
|
|
foreach (var attr in item.GetCustomAttributes(true))
|
|
{
|
|
if (item.GetValue(this) != null && item.CanRead)
|
|
writer.WriteLine(String.Format("{0}={1}",item.Name,item.GetValue(this)));
|
|
}
|
|
}
|
|
writer.Close();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageHandler.errorMessage(MessageHandler.errCodes.Config_WriteError, null);
|
|
errors++;
|
|
}
|
|
|
|
return errors;
|
|
}
|
|
|
|
public int Load()
|
|
{
|
|
int errors = 0;
|
|
try
|
|
{
|
|
foreach (var item in GetType().GetProperties())
|
|
{
|
|
foreach (var attr in item.GetCustomAttributes(true))
|
|
{
|
|
///
|
|
string line;
|
|
using (StreamReader reader = new StreamReader(openFile, true))
|
|
{
|
|
while (!string.IsNullOrEmpty(
|
|
line = reader.ReadLine()))
|
|
{
|
|
if (line.StartsWith(item.Name) && item.CanWrite)
|
|
{
|
|
string readValue = line.ToString().Replace(item.Name, "").TrimStart('=');
|
|
item.SetValue(this, Convert.ChangeType(readValue, item.PropertyType));
|
|
}
|
|
}
|
|
}
|
|
///
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
MessageHandler.errorMessage(MessageHandler.errCodes.Config_ReadError, null);
|
|
errors++;
|
|
}
|
|
|
|
return errors;
|
|
}
|
|
}
|
|
} |