test_replacevariable_test_v.../Test_ReplaceVariable.git/Form1.cs
Eugen Höglinger 52c4eec014 Initialized
2024-10-09 14:35:19 +02:00

219 lines
7.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_ReplaceVariable.git
{
public partial class Form1 : Form
{
#region Version und Copyright
// Version und Copyright
string programName = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>().Title; //Den Programmnamen auslesen
string programVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
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);
}
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 Programm-Info
/// <summary>
/// Contains the name of the program file
/// </summary>
public static string ProgramName { set; get; }
/// <summary>
/// Contains the version of the program file
/// </summary>
public static string ProgramVersion { set; get; }
/// <summary>
/// Contains the copyright notice
/// </summary>
public static string Copyright { set; get; }
private void SetProgramInfo()
{
//Name, Version und Copyright setzen
ProgramName = programName;
ProgramVersion = programVersion;
Copyright = copyright;
}
#endregion
public Form1()
{
SetProgramInfo();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
labelVersion.Text = ProgramName + "\nVersion " + ProgramVersion + "\n" + Copyright;
SetEnv();
labelBefor.Text = text;
string text1 = ReplaceVarWithValueInString(text);
if (text1 != null)
{
labelAfter.Text = text1;
}
else
{
labelAfter.Text = "Umgebungsvariable nicht gefunden";
}
}
private void SetEnv()
{
Environment.SetEnvironmentVariable("Env_Test1", "Umgebungsvariablen");
Environment.SetEnvironmentVariable("Env_Test2", "Text");
Environment.SetEnvironmentVariable("Env_Test3", "ersetzt");
//Environment.SetEnvironmentVariable("Env_Test4", "einiges");
}
const string text = "Das ist ein Text mit %Env_Test1%, welche in einem ${Env_Test2} vorkommen und %Env_Test3% werden sollen. Dabei kann ${Env_Test4} passieren.";
/// <summary>
/// Replaces the variables with the pattern ${variable} and/or %variable% in a text with the corresponding value
/// </summary>
/// <param name="text">The environment variable containing text. If a variable was not found, then the variable name is not replaced.</param>
/// <returns>The new string if successful, otherwise 'null'.</returns>
public static string ReplaceVarWithValueInString(string text)
{
if (text.Contains("${") || text.Contains("%"))
{
while (text != null && text.Contains("${"))
{
text = ReplaceVarible(text, "${", "}");
}
while (text != null && text.Contains("%"))
{
text = ReplaceVarible(text, "%", "%");
}
}
return text;
}
private static string ReplaceVarible(string text, string startSign, string endSign)
{
// Save value before variable
string temp = text.Remove(text.IndexOf(startSign));
// Extract variable
text = text.Remove(0, text.IndexOf(startSign)); // Remove 'temp'
text = text.Remove(0, startSign.Length); // Remove 'startSign'
string variableName = text.Remove(text.IndexOf(endSign)); // 'endSign' and remove the following text
// Create remaining text (behind 'endSign')
text = text.Remove(0, text.IndexOf(endSign) + 1);
// Convert variable and reassemble text
string variable = GetValueFromEnvVar(variableName);
if (variable != null)
{
return temp + variable + text; // Reassemble text
}
else
{
return null; // Reassemble text, if envvironment varible does not exist
}
}
/// <summary>
/// Get the value of an environment variable
/// </summary>
/// <param name="EnvVarName">Environment variable name</param>
/// <returns>A string if successful, otherwise 'null'.</returns>
public static string GetValueFromEnvVar(string EnvVarName)
{
if (System.Environment.GetEnvironmentVariable(EnvVarName) == null)
{
return null;
}
else
{
return System.Environment.GetEnvironmentVariable(EnvVarName);
}
}
}
}