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
///
/// Contains the name of the dll file.
///
public static string DllName { private set; get; }
///
/// Contains the version of the dll file.
///
public static string DllVersion { private set; get; }
///
/// Contains the copyright notice.
///
public static string Copyright { private set; get; }
///
/// Contains the name-, version- and copyright-information of the dll file.
///
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
}
///
/// The roaming folder
///
public string RoamingPath { private set; get; } //Pfad zum Roaming-Verzeichnis
///
/// The main key(folder)
///
public string MainKey { get; set; } //Hauptschlüssel oder Hauptordner
///
/// The sub key(folder), located under the main key
///
public string SubKey { get; set; } //Unterschlüssel oder Unterordner
///
/// The key contains the value
///
public string Key { get; set; } //Schlüssel
///
/// The value
///
public string Value { get; set; } //Wert
///
/// Writes the key
///
/// If the key could not be written
public void WriteKey()
{
string x = GenerateKey(Option.Write);
try
{
File.WriteAllText(GenerateKey(Option.Write), Value);
}
catch (Exception)
{
throw new UnableToCreateRoamingEntryException();
}
}
///
/// Reads the key
///
///
/// If the key could not be read
public string ReadKey()
{
try
{
File.ReadAllText(GenerateKey(Option.Read));
}
catch (Exception)
{
throw new UnableToReadRoamingEntryException();
}
return Value;
}
///
/// Deletes the key
///
/// If the key could not be deleted
public void DeleteKey()
{
try
{
if (File.Exists(GenerateKey(Option.Read)))
{
File.Delete(GenerateKey(Option.Read));
}
}
catch (Exception)
{
throw new UnableToDeleteRoamingEntryException();
}
}
///
/// Deletes the subkey including its content
///
/// If the subkey could not be deleted
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();
}
}
///
/// Deletes the maikey including its content
///
/// If the mainkey could not be deleted
public void DeleteMainKey()
{
try
{
if (Directory.Exists(RoamingPath + "\\" + MainKey))
{
Directory.Delete(RoamingPath + "\\" + MainKey, true);
}
}
catch (Exception)
{
throw new UnableToDeleteRoamingEntryException();
}
}
///
/// Checks whether the roaming path exists
///
/// 'True' if the roaming path exists, otherwise 'false'
public bool RoamingPathExists()
{
return Directory.Exists(RoamingPath);
}
///
/// Checks whether the main key exists
///
/// 'True' if the main key exists, otherwise 'false'
public bool MainKeyExists()
{
return Directory.Exists(RoamingPath + "\\" + MainKey);
}
///
/// Checks whether the sub key exists
///
/// 'True' if the sub key exists, otherwise 'false'
public bool SubKeyExists()
{
return Directory.Exists(RoamingPath + "\\" + MainKey + "\\" + SubKey);
}
///
/// Checks whether the key exists
///
/// 'True' if the key exists, otherwise 'false'
public bool KeyExists()
{
return Directory.Exists(RoamingPath + "\\" + MainKey + "\\" + SubKey + "\\" + Key);
}
///
/// Checks whether the key is 'null' or 'empty'
///
/// 'True' if the key is 'null' or 'empty', otherwise 'false'
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
}