using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Eugen.ESystem { public class ReplaceVariableByValue { #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(); string icon = Application.StartupPath + "\\Info.bmp"; //Assembly Datum und Zeit static DateTime value = AssemblyDateTime(); string date = value.ToShortDateString(); 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); } 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 { return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear)); } } #endregion #region DLL-Info /// /// Contains the name of the program file /// public static string DllName { set; get; } /// /// Contains the version of the program file /// public static string DllVersion { set; get; } /// /// Contains the copyright notice /// public static string Copyright { set; get; } private static void SetDllInfo() { //Name, Version und Copyright setzen DllName = dllName; DllVersion = dllVersion; Copyright = copyright; } #endregion /// /// Replaces a Environment variable through the variable value. /// static ReplaceVariableByValue() { SetDllInfo(); //Name, Version und Copyright der DLL setzen } const string repStart = "<§<§<§<"; const string repEnd = ">§>§>§>"; /// /// Replaces variables with the pattern ${variable} or %variable% with their value. /// /// The environment variable containing text. If a variable was not found, then the variable name is not replaced. /// The edited text. public static string Replace(string text) { if (!(text.Contains("${") || text.Contains("%")) || String.IsNullOrEmpty(text)) { return text; } while (text.Contains("${")) { text = ReplaceVarible(text, "${", "}"); } //Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen if (text.Contains(repStart)) { text.Replace(repStart, "${"); text.Replace(repEnd, "}"); } while (text.Contains("%")) { text = ReplaceVarible(text, "%", "%"); } //Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen if (text.Contains(repStart)) { text = text.Replace(repStart, "%"); text = text.Replace(repEnd, "%"); } return text; } /// /// Replaces variables with the pattern ${variable} or %variable% with their value. /// /// The environment variable containing text. /// Behavior if variable was not found. True=An exception is thrown. False=The variable is not replaced. /// The edited text public static string Replace(string text, bool messageWhenError) { if ((!text.Contains("${") && !text.Contains("%")) || String.IsNullOrEmpty(text)) { return text; } while (text.Contains("${")) { text = ReplaceVarible(text, "${", "}"); } //Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen if (text.Contains(repStart)) { if (messageWhenError) { string varName = text.Remove(0, text.IndexOf(repStart) + 7); varName = varName.Remove(varName.IndexOf(repEnd)); text.Replace(repStart, "${"); text.Replace(repEnd, "}"); throw new NoEnvironmentVariableFoundException(String.Concat(Language.message001seg01, varName, Language.message001seg02)); } else { text.Replace(repStart, "${"); text.Replace(repEnd, "}"); } } while (text.Contains("%")) { text = ReplaceVarible(text, "%", "%"); } //Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen if (text.Contains(repStart)) { if (messageWhenError) { string varName = text.Remove(0, text.IndexOf(repStart) + 7); varName = varName.Remove(varName.IndexOf(repEnd)); text = text.Replace(repStart, "%"); text = text.Replace(repEnd, "%"); throw new NoEnvironmentVariableFoundException(String.Concat(Language.message001seg01, varName, Language.message001seg02)); } else { text = text.Replace(repStart, "%"); text = text.Replace(repEnd, "%"); } } return text; } private static string ReplaceVarible(string text, string startSign, string endSign) { //Wert vor der Variablen sichern string temp = text.Remove(text.IndexOf(startSign)); //Variable extrahieren text = text.Remove(0, text.IndexOf(startSign)); //'temp' entfernen text = text.Remove(0, startSign.Length); //'startSign' entfernen string variableName = text.Remove(text.IndexOf(endSign)); //'endSign' und nachfolgenden Text entfernen //Restlicher Text (hinter 'endSign') erzeugen text = text.Remove(0, text.IndexOf(endSign) + 1); //Variable umwandeln und Text wieder zusammenbauen string variable = ReplaceEnvironmentVariable(variableName); text = temp + variable + text; //Text wieder zusammensetzen return text; } private static string ReplaceEnvironmentVariable(string variableName) { string variableValue = ""; //Wenn im Text eine bekannte Umgebungsvariable enthalten ist, dann gegen den aktuellen Wert tauschen variableValue = Environment.GetEnvironmentVariable(variableName); if (String.IsNullOrEmpty(variableValue)) { variableValue = String.Concat(repStart, variableName, repEnd); //Wird keine Umgebungsvariable gefunden, dann wieder den Variblen Namen zurückgeben } return variableValue; } } #region Exceptions class NoEnvironmentVariableFoundException : ApplicationException { public NoEnvironmentVariableFoundException() { } public NoEnvironmentVariableFoundException(string message) : base(message) { } public NoEnvironmentVariableFoundException(string message, Exception inner) : base(message, inner) { } } #endregion }