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; namespace Eugen.ESystem { public class VaribleThroughString { #region Version und Copyright // Version und Copyright string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); string copyright = ((AssemblyCopyrightAttribute)attributes[0]).Copyright; //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; } #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 void SetDllInfo() { //Name, Version und Copyright setzen DllName = dllName; DllVersion = dllVersion; Copyright = this.copyright; string[] dllInfo = new string[3]; dllInfo[0] = dllName; dllInfo[1] = dllVersion; dllInfo[2] = this.copyright; DllInfo = dllInfo; } #endregion /// /// Replaces a Environment variable through the variable value. /// public VaribleThroughString() { 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 }