193 lines
5.3 KiB
C#
193 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Test_EnvSetzen
|
|
{
|
|
class ReplaceVariable
|
|
{
|
|
/// <summary>
|
|
/// Replaces variables in a string with values.
|
|
/// If a variable is not found, an exception is thrown
|
|
/// </summary>
|
|
public ReplaceVariable()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Replaces all variables in the transferred string with values.
|
|
/// Variable names are not case sensitive.
|
|
/// Locally defined variables take precedence over environment variables.
|
|
/// </summary>
|
|
/// <param name="text">Text with variables. Variables must have DOS or Unix format. E.g.: %dos% or ${unix}</param>
|
|
/// <returns></returns>
|
|
public string ExtractVariable(string text)
|
|
{
|
|
string startSign = "";
|
|
string endSign = "";
|
|
string newText = "";
|
|
|
|
//Wenn im Text eine bekannte Variable enthalten ist, dann gegen den aktuellen Wert tauschen
|
|
if (!string.IsNullOrEmpty(text))
|
|
{
|
|
while (text.Contains("%") || text.Contains("{"))
|
|
{
|
|
if (text.Contains("%"))
|
|
{
|
|
startSign = "%";
|
|
endSign = "%";
|
|
}
|
|
else
|
|
{
|
|
startSign = "${";
|
|
endSign = "}";
|
|
}
|
|
|
|
newText = ReplaceVariableThroughText(text, startSign, endSign); //ENV-Variablen extrahieren
|
|
|
|
//Abbrechen wenn die Umgebungsvariable nicht gefunden wurde
|
|
if (String.IsNullOrEmpty(newText))
|
|
{
|
|
text = "";
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
text = newText;
|
|
}
|
|
}
|
|
}
|
|
return text;
|
|
}
|
|
|
|
private string ReplaceVariableThroughText(string text, string startSign, string endSign)
|
|
{
|
|
string textTemp = "";
|
|
string textAnfang = "";
|
|
string textEnde = "";
|
|
string variableName = "";
|
|
string variableValue = "";
|
|
|
|
//'Textanfang' extrahieren
|
|
textAnfang = text.Remove(text.IndexOf(startSign));
|
|
|
|
textTemp = text.Remove(0, text.IndexOf(startSign));
|
|
|
|
//Variable extrahieren
|
|
variableName = textTemp.Remove(0, textTemp.IndexOf(startSign) + startSign.Length); //'startSign' entfernen
|
|
variableName = variableName.Remove(variableName.IndexOf(endSign)); //'endSign' und nachfolgenden Text entfernen
|
|
|
|
//Textende extrahieren
|
|
textEnde = textTemp.Remove(0, startSign.Length); //'startSign' entfernen
|
|
textEnde = textEnde.Remove(0, textEnde.IndexOf(endSign) + 1); //Alle Zeichen bis zum 'endSign' entfernen
|
|
|
|
//Variable umwandeln und Text wieder zusammenbauen
|
|
variableValue = ConvertVariableToString(variableName);
|
|
|
|
if (String.IsNullOrEmpty(variableValue))
|
|
{
|
|
text = ""; //Wenn die Variable nicht konvertiert werden konnte, dann einen leeren Text zurückgeben
|
|
// throw new VariableValueNotFoundException(variableName); //Wird diese Zeile auskommentiert, dann wird ein leerer Text zurückgegeben
|
|
}
|
|
else
|
|
{
|
|
text = String.Concat(textAnfang, variableValue, textEnde); //Text zusammensetzen
|
|
}
|
|
|
|
return text;
|
|
}
|
|
|
|
private string ConvertVariableToString(string variableName)
|
|
{
|
|
string variableValue = "";
|
|
bool localVariableExist = false;
|
|
|
|
//Sind lokale Variablen definiert, dann zuerst diese durchsuchen
|
|
if (variables != null)
|
|
{
|
|
foreach (var varName in variables)
|
|
{
|
|
if (varName.VariableName.ToUpper() == variableName.ToUpper())
|
|
{
|
|
variableValue = varName.VariableValue;
|
|
localVariableExist = true;
|
|
break; //Wurde eine Entsprechung gefunden, dann nicht mehr weitersuchen
|
|
}
|
|
}
|
|
}
|
|
|
|
//Wurde in den lokalen Variablen nichts gefunden, dann die Umgebungsvariablen durchsuchen
|
|
if (!localVariableExist)
|
|
{
|
|
if (!localVariableExist & Environment.GetEnvironmentVariable(variableName) != null)
|
|
{
|
|
variableValue = Environment.GetEnvironmentVariable(variableName); //Wert aus Umgebungsvariable holen
|
|
}
|
|
else
|
|
{
|
|
variableValue = ""; //Wenn es keine passende Variable gibt, dann einen leeren Wert übergeben
|
|
}
|
|
}
|
|
|
|
return variableValue;
|
|
}
|
|
|
|
Variable variable = new Variable();
|
|
static List<Variable> variables = new List<Variable>();
|
|
|
|
/// <summary>
|
|
/// Adds a local defined variable to the variable list.
|
|
/// </summary>
|
|
/// <param name="variableName">A valid variable Name.</param>
|
|
/// <param name="variableValue">A valid variable value.</param>
|
|
public void Add(string variableName, string variableValue)
|
|
{
|
|
Variable tempVariable = new Variable();
|
|
tempVariable.VariableName = variableName;
|
|
tempVariable.VariableValue = variableValue;
|
|
variables.Add(tempVariable);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the first variable found with the specified name from the list.
|
|
/// The variable name is not case sensitive.
|
|
/// </summary>
|
|
/// <param name="variableName">A valid variable Name.</param>
|
|
public void Remove(string variableName)
|
|
{
|
|
foreach (var varName in variables)
|
|
{
|
|
if (varName.VariableName.ToUpper() == variableName.ToUpper())
|
|
{
|
|
variables.Remove(varName);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Empty the complete variable list.
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
variables.Clear();
|
|
}
|
|
}
|
|
|
|
#region Exception
|
|
public class VariableValueNotFoundException : ApplicationException
|
|
{
|
|
public VariableValueNotFoundException()
|
|
{ }
|
|
|
|
public VariableValueNotFoundException(string message) : base(message)
|
|
{ }
|
|
|
|
public VariableValueNotFoundException(string message, Exception inner) : base(message, inner)
|
|
{ }
|
|
}
|
|
#endregion
|
|
}
|