442 lines
14 KiB
C#
442 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Security.Permissions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
namespace Eugen.ESystem
|
|
{
|
|
public class Roaming
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
|
|
static string copyright = GenerateCopyright();
|
|
|
|
//Assembly Datum und Zeit
|
|
static DateTime value = AssemblyDateTime();
|
|
static string date = value.ToShortDateString();
|
|
static string time = value.ToLongTimeString();
|
|
|
|
private static DateTime AssemblyDateTime()
|
|
{
|
|
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
|
|
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
|
|
return buildDateTime;
|
|
}
|
|
|
|
private static string CurrentYear()
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
|
|
private static string StartYear()
|
|
{
|
|
string startYear = "";
|
|
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
|
|
{
|
|
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
|
|
while (startYear.StartsWith(" "))
|
|
{
|
|
startYear = startYear.Remove(0, 1);
|
|
}
|
|
|
|
if (startYear.Contains("-"))
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf("-"));
|
|
}
|
|
else
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf(" "));
|
|
}
|
|
return startYear;
|
|
}
|
|
else
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
}
|
|
|
|
private static string GenerateCopyright()
|
|
{
|
|
string startYear = StartYear();
|
|
string currentYear = CurrentYear();
|
|
|
|
if (startYear == currentYear)
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
}
|
|
else
|
|
{
|
|
string temp = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
string start = temp.Remove(temp.IndexOf(startYear) - 1);
|
|
string end = (temp.Remove(0, temp.IndexOf(" by")));
|
|
|
|
return String.Concat(start, " ", startYear, "-", currentYear, end);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DLL-Info
|
|
/// <summary>
|
|
/// Contains the name of the dll file.
|
|
/// </summary>
|
|
public static string DllName { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the dll file.
|
|
/// </summary>
|
|
public static string DllVersion { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice.
|
|
/// </summary>
|
|
public static string Copyright { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the name-, version- and copyright-information of the dll file.
|
|
/// </summary>
|
|
public static string[] DllInfo { private set; get; }
|
|
|
|
private static void SetDllInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
DllName = dllName;
|
|
DllVersion = dllVersion;
|
|
Copyright = copyright;
|
|
|
|
string[] dllInfo = new string[3];
|
|
dllInfo[0] = dllName;
|
|
dllInfo[1] = dllVersion;
|
|
dllInfo[2] = copyright;
|
|
DllInfo = dllInfo;
|
|
}
|
|
|
|
protected string ProgramName { private set; get; }
|
|
protected string ProgramVersion { private set; get; }
|
|
protected string ProgramCopyright { private set; get; }
|
|
#endregion
|
|
|
|
static Roaming()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
public Roaming()
|
|
{
|
|
SetDllInfo();
|
|
RoamingPath = GetAppData();
|
|
}
|
|
|
|
enum Option
|
|
{
|
|
Write,
|
|
Read
|
|
}
|
|
|
|
/// <summary>
|
|
/// The roaming folder
|
|
/// </summary>
|
|
public string RoamingPath { private set; get; } //Pfad zum Roaming-Verzeichnis
|
|
|
|
/// <summary>
|
|
/// The main key(folder)
|
|
/// </summary>
|
|
public string MainKey { get; set; } //Hauptschlüssel oder Hauptordner
|
|
|
|
/// <summary>
|
|
/// The sub key(folder), located under the main key
|
|
/// </summary>
|
|
public string SubKey { get; set; } //Unterschlüssel oder Unterordner
|
|
|
|
/// <summary>
|
|
/// The key contains the value
|
|
/// </summary>
|
|
public string Key { get; set; } //Schlüssel
|
|
|
|
/// <summary>
|
|
/// The value
|
|
/// </summary>
|
|
public string Value { get; set; } //Wert
|
|
|
|
/// <summary>
|
|
/// Writes the key
|
|
/// </summary>
|
|
/// <exception cref="UnableToCreateRoamingEntryException">If the key could not be written</exception>
|
|
public void WriteKey()
|
|
{
|
|
string x = GenerateKey(Option.Write);
|
|
try
|
|
{
|
|
File.WriteAllText(GenerateKey(Option.Write), Value);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new UnableToCreateRoamingEntryException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the key
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <exception cref="UnableToReadRoamingEntryException">If the key could not be read</exception>
|
|
public string ReadKey()
|
|
{
|
|
try
|
|
{
|
|
File.ReadAllText(GenerateKey(Option.Read));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new UnableToReadRoamingEntryException();
|
|
}
|
|
return Value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the key
|
|
/// </summary>
|
|
/// <exception cref="UnableToDeleteRoamingEntryException">If the key could not be deleted</exception>
|
|
public void DeleteKey()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(GenerateKey(Option.Read)))
|
|
{
|
|
File.Delete(GenerateKey(Option.Read));
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new UnableToDeleteRoamingEntryException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the subkey including its content
|
|
/// </summary>
|
|
/// <exception cref="UnableToDeleteRoamingEntryException">If the subkey could not be deleted</exception>
|
|
public void DeleteSubKey()
|
|
{
|
|
try
|
|
{
|
|
if (!String.IsNullOrEmpty(SubKey))
|
|
{
|
|
if (Directory.Exists(RoamingPath + "\\" + MainKey + "\\" + SubKey))
|
|
{
|
|
Directory.Delete(RoamingPath + "\\" + MainKey + "\\" + SubKey, true);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new UnableToDeleteRoamingEntryException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the maikey including its content
|
|
/// </summary>
|
|
/// <exception cref="UnableToDeleteRoamingEntryException">If the mainkey could not be deleted</exception>
|
|
public void DeleteMainKey()
|
|
{
|
|
try
|
|
{
|
|
if (Directory.Exists(RoamingPath + "\\" + MainKey))
|
|
{
|
|
Directory.Delete(RoamingPath + "\\" + MainKey, true);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new UnableToDeleteRoamingEntryException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the roaming path exists
|
|
/// </summary>
|
|
/// <returns>'True' if the roaming path exists, otherwise 'false'</returns>
|
|
public bool RoamingPathExists()
|
|
{
|
|
return Directory.Exists(RoamingPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the main key exists
|
|
/// </summary>
|
|
/// <returns>'True' if the main key exists, otherwise 'false'</returns>
|
|
public bool MainKeyExists()
|
|
{
|
|
return Directory.Exists(RoamingPath + "\\" + MainKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the sub key exists
|
|
/// </summary>
|
|
/// <returns>'True' if the sub key exists, otherwise 'false'</returns>
|
|
public bool SubKeyExists()
|
|
{
|
|
return Directory.Exists(RoamingPath + "\\" + MainKey + "\\" + SubKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the key exists
|
|
/// </summary>
|
|
/// <returns>'True' if the key exists, otherwise 'false'</returns>
|
|
public bool KeyExists()
|
|
{
|
|
if (SubKeyExists())
|
|
{
|
|
return Directory.Exists(RoamingPath + "\\" + MainKey + "\\" + SubKey + "\\" + Key);
|
|
}
|
|
else
|
|
{
|
|
return Directory.Exists(RoamingPath + "\\" + MainKey + "\\" + Key);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the key is 'null' or 'empty'
|
|
/// </summary>
|
|
/// <returns>'True' if the key is 'null' or 'empty', otherwise 'false'</returns>
|
|
public bool ValueIsNullOrEmpty()
|
|
{
|
|
if (KeyExists())
|
|
{
|
|
if (String.IsNullOrEmpty(ReadKey()))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private string GetAppData()
|
|
{
|
|
return Environment.GetEnvironmentVariable("APPDATA"); //Ermittelt den Pafad des Verzeichnisses '%APPDATA%\Roaming'
|
|
}
|
|
|
|
private string GenerateKey(Option option)
|
|
{
|
|
string key = "";
|
|
|
|
try
|
|
{
|
|
if (!Directory.Exists(RoamingPath + "\\" + MainKey))
|
|
{
|
|
if (option == Option.Write)
|
|
{
|
|
Directory.CreateDirectory(RoamingPath + "\\" + MainKey);
|
|
}
|
|
else
|
|
{
|
|
throw new UnableToReadRoamingEntryException();
|
|
}
|
|
}
|
|
|
|
if (String.IsNullOrEmpty(SubKey))
|
|
{
|
|
SubKey = "";
|
|
}
|
|
else
|
|
{
|
|
if (option == Option.Write)
|
|
{
|
|
if (!Directory.Exists(RoamingPath + "\\" + MainKey + "\\" + SubKey))
|
|
{
|
|
Directory.CreateDirectory(RoamingPath + "\\" + MainKey + "\\" + SubKey);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (SubKey == "")
|
|
{
|
|
key = RoamingPath + "\\" + MainKey + "\\" + Key;
|
|
}
|
|
else
|
|
{
|
|
key = RoamingPath + "\\" + MainKey + "\\" + SubKey + "\\" + Key;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new UnableToGenerateKeyException();
|
|
}
|
|
|
|
return key;
|
|
}
|
|
}
|
|
|
|
#region Exceptions
|
|
class UnableToCreateRoamingEntryException : ApplicationException
|
|
{
|
|
public UnableToCreateRoamingEntryException()
|
|
{ }
|
|
|
|
public UnableToCreateRoamingEntryException(string message) : base(message)
|
|
{ }
|
|
|
|
public UnableToCreateRoamingEntryException(string message, Exception inner) : base(message, inner)
|
|
{ }
|
|
}
|
|
|
|
|
|
class UnableToReadRoamingEntryException : ApplicationException
|
|
{
|
|
public UnableToReadRoamingEntryException()
|
|
{ }
|
|
|
|
public UnableToReadRoamingEntryException(string message) : base(message)
|
|
{ }
|
|
|
|
public UnableToReadRoamingEntryException(string message, Exception inner) : base(message, inner)
|
|
{ }
|
|
}
|
|
|
|
|
|
class UnableToDeleteRoamingEntryException : ApplicationException
|
|
{
|
|
public UnableToDeleteRoamingEntryException()
|
|
{ }
|
|
|
|
public UnableToDeleteRoamingEntryException(string message) : base(message)
|
|
{ }
|
|
|
|
public UnableToDeleteRoamingEntryException(string message, Exception inner) : base(message, inner)
|
|
{ }
|
|
}
|
|
|
|
|
|
class UnableToGenerateKeyException : ApplicationException
|
|
{
|
|
public UnableToGenerateKeyException()
|
|
{ }
|
|
|
|
public UnableToGenerateKeyException(string message) : base(message)
|
|
{ }
|
|
|
|
public UnableToGenerateKeyException(string message, Exception inner) : base(message, inner)
|
|
{ }
|
|
}
|
|
#endregion
|
|
}
|