versioncomparison_dll_visua.../VersionComparison/VersionComparison.cs
Eugen Höglinger e388537531 Initialized
2024-10-11 16:24:59 +02:00

214 lines
7.4 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Eugen.ESystem
{
public class VersionComparison
{
#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
static VersionComparison()
{
SetDllInfo();
}
/// <summary>
/// Compare a specified version with the version of an app
/// </summary>
/// <param name="version">String formatted as 'Majorversion.Minorversion.Buildnumber.Revision'.</param>
/// <param name="appVersion">A valid App version, e.g.: ShowTextFile.DllVersion</param>
/// <returns>-1 = No valid comparison
/// 0 = Both versions the same
/// 1 = 'version' is newer than app version
/// 2 = Version of the app is newer than 'version'</returns>
public static int Compare(string version, string appVersion)
{
if (!IsVersionValid(version) || String.IsNullOrEmpty(appVersion))
{
return -1;
}
string[] versionSegment = version.Split('.');
string[] appVersionSegment = appVersion.Split('.');
if (version == appVersion)
{
return 0; // Versionen sind gleich
}
// Vergleich Hauptversion
if (Convert.ToInt16(versionSegment[0]) > Convert.ToInt16(appVersionSegment[0]))
{
return 1; //'version' neuer als 'appVersion'
}
if (Convert.ToInt16(versionSegment[0]) < Convert.ToInt16(appVersionSegment[0]))
{
return 2; //'appVersion' neuer als 'version'
}
// Vergleich Nebenversion
if (Convert.ToInt16(versionSegment[1]) > Convert.ToInt16(appVersionSegment[1]))
{
return 1; //'version' neuer als 'appVersion'
}
if (Convert.ToInt16(versionSegment[1]) < Convert.ToInt16(appVersionSegment[1]))
{
return 2; //'appVersion' neuer als 'version'
}
// Vergleich Buildnummer
if (Convert.ToInt16(versionSegment[2]) > Convert.ToInt16(appVersionSegment[21]))
{
return 1; //'version' neuer als 'appVersion'
}
if (Convert.ToInt16(versionSegment[2]) < Convert.ToInt16(appVersionSegment[2]))
{
return 2; //'appVersion' neuer als 'version'
}
// Vergleich Revision
if (Convert.ToInt16(versionSegment[3]) > Convert.ToInt16(appVersionSegment[3]))
{
return 1; //'version' neuer als 'appVersion'
}
if (Convert.ToInt16(versionSegment[3]) < Convert.ToInt16(appVersionSegment[3]))
{
return 2; //'appVersion' neuer als 'version'
}
return -1; //Kein gültiger Vergleich
}
private static bool IsVersionValid(string version)
{
if (String.IsNullOrEmpty(version) || CountPoints(version) != 3)
{
return false;
}
else
{
return true;
}
}
private static int CountPoints(string version)
{
return version.Count(c => c == '.'); //Zeichen zählen mit Linq
}
}
}