aboutbox_dll_visualstudio/AboutBox/AboutBox.cs
2020-11-25 16:52:55 +01:00

354 lines
12 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.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Eugen.ESystem.Windows.Forms
{
/// <summary>
/// Displays a Program Information Box
/// </summary>
public partial class AboutBox : Form
{
#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
/// <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;
}
#endregion
/// <summary>
/// Displays a Program Information Box
/// </summary>
static AboutBox()
{
SetDllInfo(); //Name, Version und Copyright der DLL setzen
}
/// <summary>
/// Displays a Program Information Box
/// </summary>
/// <param name="programName">A valid Program name.</param>
/// <param name="programVersion">A valid version number.</param>
/// <param name="programCopyright">The copyrigth Information</param>
/// <param name="showDllInfoButton">true = Button is displayed; false = Button is not displayed.</param>
public AboutBox(string programName, string programVersion, string programCopyright)
{
InitializeComponent();
SetContent(programName, programVersion, programCopyright); //Fensterinhalt setzen
}
public AboutBox(string programName, string programVersion, string programCopyright, bool showDllInfoButton)
{
InitializeComponent();
if (showDllInfoButton)
{
buttonDllInfo.Enabled = true;
buttonDllInfo.Visible = true;
}
else
{
buttonDllInfo.Enabled = false;
buttonDllInfo.Visible = false;
}
SetContent(programName, programVersion, programCopyright); //Fensterinhalt setzen
}
/// <summary>
/// Displays a Program Information Box
/// </summary>
/// <param name="programName">A valid Program name.</param>
/// <param name="programVersion">A valid version number.</param>
/// <param name="programCopyright">The copyrigth Information</param>
/// <param name="icon"> valid icon in the size 32x32 dots with path. Larger icons are cropped. E.g.: C\:MyFolder\MyIcon.ico</param>
/// <param name="showDllInfoButton">true = Button is displayed; false = Button is not displayed.</param>
public AboutBox(string programName, string programVersion, string programCopyright, Icon icon, bool showDllInfoButton)
{
InitializeComponent();
pictureBox.Image = Bitmap.FromHicon(new Icon(icon, new Size(32, 32)).Handle);
if (showDllInfoButton)
{
buttonDllInfo.Enabled = true;
buttonDllInfo.Visible = true;
}
else
{
buttonDllInfo.Enabled = false;
buttonDllInfo.Visible = false;
}
SetContent(programName, programVersion, programCopyright); //Fensterinhalt setzen
}
private void AboutBox_Load(object sender, EventArgs e)
{
DllInfoSelected = false;
}
protected string ProgramName { private set; get; }
protected string ProgramVersion { private set; get; }
protected string ProgramCopyright { private set; get; }
protected bool DllInfoSelected { set; get; }
protected bool ShowDllInfoButton { set; get; }
private void buttonClose_Click(object sender, EventArgs e)
{
if (DllInfoSelected)
{
buttonDllInfo.Enabled = true;
labelDisclaimer.Text = Language.disclaimerHeader001;
textBox.Text = Language.disclaimer;
DllInfoSelected = false;
}
else
{
this.Close(); //Das Fenster schliessen
}
}
private void buttonDllInfo_Click(object sender, EventArgs e)
{
//DLL-Informationen anzeigen
DllInfoSelected = true;
buttonDllInfo.Enabled = false;
labelDisclaimer.Text = Language.dllInfo001;
textBox.Text = "";
if (GetAllDllNames() != null)
{
textBox.Text = CreateDllInfo(GetAllDllNames());
}
}
private void buttonExport_Click(object sender, EventArgs e)
{
//Speichert den Inhalt der TextBox oder den selektierten Bereich daraus
saveFileDialog.Filter = Language.dialog001;
saveFileDialog.ShowDialog();
}
private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
{
string fileName = saveFileDialog.FileName;
string text = ExportText();
try
{
File.WriteAllText(fileName, text); //Schreibt den Text in eine Datei
}
catch (Exception)
{
MessageBox.Show(Language.message001,
Language.messageHead001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
private void SaveFileDialogFileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string fileName = saveFileDialog.FileName;
string text = ExportText();
try
{
File.WriteAllText(fileName, text); //Schreibt den Text in eine Datei
}
catch (Exception)
{
MessageBox.Show(Language.message001,
Language.messageHead001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
private void SetContent(string programName, string programVersion, string programCopyright)
{
labelProgramName.Text = programName;
ProgramName = programName;
if (programVersion.StartsWith("V"))
{
labelVersion.Text = programVersion;
ProgramVersion = programVersion;
}
else
{
labelVersion.Text = String.Concat("Version ", programVersion);
ProgramVersion = labelVersion.Text;
}
labelCopyright.Text = programCopyright;
ProgramCopyright = programCopyright;
textBox.Text = Language.disclaimer;
}
private string ExportText()
{
if (DllInfoSelected)
{
DateTime today = DateTime.Now;
return String.Concat(Language.dllInfo001, "\r\n", today.ToString("yyyy-MM-dd - HH:mm:ss"), "\r\n--------------------------------------------------------------------------------\r\n", FixLineLenght.Format(textBox.Text, 80)); //Gibt den ganzen Text zurück
}
else
{
if (textBox.SelectionLength > 0)
{
return textBox.SelectedText; //Gibt den selektierten Text zurück
}
else
{
return String.Concat(ProgramName, "\r\n", ProgramVersion, "\r\n", ProgramCopyright, "\r\n--------------------------------------------------------------------------------\r\n", FixLineLenght.Format(textBox.Text, 80)); //Gibt den ganzen Text zurück
}
}
}
private List<string> GetAllDllNames()
{
List<string> dllNames = new List<string>();
if (dllNames != null)
{
dllNames.Clear();
}
DirectoryInfo ParentDirectory = new DirectoryInfo(Application.StartupPath);
foreach (FileInfo fi in ParentDirectory.GetFiles())
{
if (fi.Extension.ToLower() == ".dll")
{
dllNames.Add(fi.FullName);
}
}
return dllNames;
}
private string CreateDllInfo(List<string> dllNames)
{
string dllInfos = "";
string dllName = "";
string dllVersion = "";
if (dllNames != null)
{
foreach (var name in dllNames)
{
Assembly assembly = Assembly.LoadFrom(name);
dllName = String.Concat(assembly.ToString().Remove(assembly.ToString().IndexOf(",")), ".dll");
dllVersion = assembly.ToString().Remove(0, assembly.ToString().IndexOf("Version=") + 8);
dllVersion = dllVersion.Remove(dllVersion.IndexOf(","));
dllInfos = String.Concat(dllInfos, dllName, "\r\n", dllVersion, "\r\n\r\n");
}
}
return dllInfos.Remove(dllInfos.LastIndexOf("\r\n\r\n"));
}
}
}