313 lines
10 KiB
C#
313 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Win32;
|
|
|
|
namespace Eugen.ESystem
|
|
{
|
|
/// <summary>
|
|
/// Description of Registry.
|
|
/// </summary>
|
|
public class Registry
|
|
{
|
|
#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
|
|
|
|
public enum RegMainKey
|
|
{
|
|
ClassesRoot,
|
|
CurrentUser,
|
|
LocalMachine,
|
|
Users,
|
|
CurrentConfig
|
|
}
|
|
|
|
static Registry()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
public Registry()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
// Create a Subkey
|
|
public RegMainKey MainKey { get; set; }
|
|
public string SubKey { get; set; }
|
|
public string Key { get; set; }
|
|
public string Value { get; set; }
|
|
public bool Error { get; set; }
|
|
|
|
/// <summary>
|
|
/// Write Values to the Subkey
|
|
/// </summary>
|
|
public void WriteSubkey()
|
|
{
|
|
// Write Values to the Subkey
|
|
Microsoft.Win32.RegistryKey mKey = null;
|
|
switch (MainKey)
|
|
{
|
|
case RegMainKey.ClassesRoot:
|
|
mKey = Microsoft.Win32.Registry.ClassesRoot;
|
|
break;
|
|
case RegMainKey.CurrentUser:
|
|
mKey = Microsoft.Win32.Registry.CurrentUser;
|
|
break;
|
|
case RegMainKey.LocalMachine:
|
|
mKey = Microsoft.Win32.Registry.LocalMachine;
|
|
break;
|
|
case RegMainKey.Users:
|
|
mKey = Microsoft.Win32.Registry.Users;
|
|
break;
|
|
case RegMainKey.CurrentConfig:
|
|
mKey = Microsoft.Win32.Registry.CurrentConfig;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
RegistryKey newKey = mKey.CreateSubKey(SubKey);
|
|
newKey.SetValue(Key, Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read Values from the Subkey
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception">Subkey does not exist exception</exception>
|
|
public string ReadSubkey()
|
|
{
|
|
// Read Values from the Subkey
|
|
Microsoft.Win32.RegistryKey mKey = null;
|
|
switch (MainKey)
|
|
{
|
|
case RegMainKey.ClassesRoot:
|
|
mKey = Microsoft.Win32.Registry.ClassesRoot;
|
|
break;
|
|
case RegMainKey.CurrentUser:
|
|
mKey = Microsoft.Win32.Registry.CurrentUser;
|
|
break;
|
|
case RegMainKey.LocalMachine:
|
|
mKey = Microsoft.Win32.Registry.LocalMachine;
|
|
break;
|
|
case RegMainKey.Users:
|
|
mKey = Microsoft.Win32.Registry.Users;
|
|
break;
|
|
case RegMainKey.CurrentConfig:
|
|
mKey = Microsoft.Win32.Registry.CurrentConfig;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (SubKeyExist(SubKey))
|
|
{
|
|
RegistryKey myKey = mKey.OpenSubKey(SubKey);
|
|
return (string)myKey.GetValue(Key);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Subkey does not exist!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete the Subkey
|
|
/// </summary>
|
|
/// <exception cref="Exception">Subkey to be deleted</exception>
|
|
public void DeleteSubkey()
|
|
{
|
|
// Delete the Subkey
|
|
Microsoft.Win32.RegistryKey mKey = null;
|
|
switch (MainKey)
|
|
{
|
|
case RegMainKey.ClassesRoot:
|
|
mKey = Microsoft.Win32.Registry.ClassesRoot;
|
|
break;
|
|
case RegMainKey.CurrentUser:
|
|
mKey = Microsoft.Win32.Registry.CurrentUser;
|
|
break;
|
|
case RegMainKey.LocalMachine:
|
|
mKey = Microsoft.Win32.Registry.LocalMachine;
|
|
break;
|
|
case RegMainKey.Users:
|
|
mKey = Microsoft.Win32.Registry.Users;
|
|
break;
|
|
case RegMainKey.CurrentConfig:
|
|
mKey = Microsoft.Win32.Registry.CurrentConfig;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
//string MyKey = SubKey.Remove(SubKey.LastIndexOf('\\'));
|
|
|
|
if (SubKeyExist(SubKey))
|
|
{
|
|
mKey.DeleteSubKeyTree(SubKey);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Subkey does not exist!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if a Subkey exist
|
|
/// </summary>
|
|
/// <param name="Subkey">Subkey to be queried</param>
|
|
/// <returns></returns>
|
|
public bool SubKeyExist(string Subkey)
|
|
{
|
|
// Check if a Subkey exist
|
|
Microsoft.Win32.RegistryKey mKey = null;
|
|
switch (MainKey)
|
|
{
|
|
case RegMainKey.ClassesRoot:
|
|
mKey = Microsoft.Win32.Registry.ClassesRoot;
|
|
break;
|
|
case RegMainKey.CurrentUser:
|
|
mKey = Microsoft.Win32.Registry.CurrentUser;
|
|
break;
|
|
case RegMainKey.LocalMachine:
|
|
mKey = Microsoft.Win32.Registry.LocalMachine;
|
|
break;
|
|
case RegMainKey.Users:
|
|
mKey = Microsoft.Win32.Registry.Users;
|
|
break;
|
|
case RegMainKey.CurrentConfig:
|
|
mKey = Microsoft.Win32.Registry.CurrentConfig;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
RegistryKey myKey = mKey.OpenSubKey(Subkey);
|
|
|
|
if (myKey == null)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|