aboutbox_dll_visualstudio/AboutBox/AboutBox.cs
Eugen Höglinger 46c64b0132 Änderung
- Abfrage eingefügt, die den Button 'DLL-Info' unterdrückt, wenn
   die EXE und die DLLs in eine EXE gepackt sind.
   Das verhindert einen Fehler, weil keine DLL gefunden werden.
2021-06-01 17:08:23 +02:00

573 lines
20 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
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>
public AboutBox(string programName, string programVersion, string programCopyright)
{
InitializeComponent();
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="width">Window with: Min. 500, Max. 800.</param>
/// <param name="hight">Window hight: Min. 400, Max. 600.</param>
public AboutBox(string programName, string programVersion, string programCopyright, int width, int hight)
{
InitializeComponent();
//Fenstergröße einstellen
SetWindow(width, hight);
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="showDllInfoButton">true = Button is displayed; false = Button is not displayed.</param>
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="showDllInfoButton">true = Button is displayed; false = Button is not displayed.</param>
/// <param name="width">Window with: Min. 500, Max. 800.</param>
/// <param name="hight">Window hight: Min. 400, Max. 600.</param>
public AboutBox(string programName, string programVersion, string programCopyright, bool showDllInfoButton, int width, int hight)
{
InitializeComponent();
//Fenstergröße einstellen
SetWindow(width, hight);
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">A 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
}
/// <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">A 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>
/// <param name="width">Window with: Min. 500, Max. 800.</param>
/// <param name="hight">Window hight: Min. 400, Max. 600.</param>
public AboutBox(string programName, string programVersion, string programCopyright, Icon icon, bool showDllInfoButton, int width, int hight)
{
InitializeComponent();
//Fenstergröße einstellen
SetWindow(width, hight);
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;
Appearance();
}
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;
buttonDllInfo.Visible = true;
radioButtonAll.Enabled = false;
radioButtonAll.Visible = false;
radioButtonInternal.Enabled = false;
radioButtonInternal.Visible = false;
textBox.WordWrap = true;
textBox.ScrollBars = ScrollBars.Vertical;
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;
buttonDllInfo.Visible = false;
radioButtonAll.Enabled = true;
radioButtonAll.Visible = true;
radioButtonInternal.Enabled = true;
radioButtonInternal.Visible = true;
textBox.WordWrap = false;
textBox.ScrollBars = ScrollBars.Both;
labelDisclaimer.Text = Language.dllInfo001;
textBox.Text = "";
textBox.Text = CreateDllInfo();
}
private void radioButtonAll_CheckedChanged(object sender, EventArgs e)
{
textBox.Text = CreateDllInfo();
}
private void radioButtonInternal_CheckedChanged(object sender, EventArgs e)
{
textBox.Text = CreateDllInfo();
}
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;
if (radioButtonAll.Checked)
{
return String.Concat(Language.dllInfo002, "\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
{
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> dlls = new List<string>();
if (dlls != null)
{
dlls.Clear();
}
dlls = GetGlobalDllNames();
if (GetInternalDllNames() != null)
{
dlls.AddRange(GetInternalDllNames());
}
dlls.Sort();
return dlls;
}
private List<string> GetGlobalDllNames()
{
List<string> dlls = new List<string>();
if (dlls != null)
{
dlls.Clear();
}
foreach (ProcessModule module in Process.GetCurrentProcess().Modules)
{
dlls.Add(module.ModuleName + Environment.NewLine + module.FileVersionInfo.ProductVersion + Environment.NewLine + module.FileName);
}
dlls.Sort();
return dlls;
}
private List<string> GetInternalDllNames()
{
List<string> dlls = new List<string>();
string dllVersion = "";
if (dlls != null)
{
dlls.Clear();
}
DirectoryInfo ParentDirectory = new DirectoryInfo(Application.StartupPath);
foreach (FileInfo fi in ParentDirectory.GetFiles())
{
if (fi.Extension.ToLower() == ".dll")
{
Assembly assembly = Assembly.LoadFrom(fi.FullName.Remove(0, fi.FullName.LastIndexOf("\\") + 1));
dllVersion = assembly.ToString().Remove(0, assembly.ToString().IndexOf("Version=") + 8);
dllVersion = dllVersion.Remove(dllVersion.IndexOf(","));
dlls.Add(String.Concat(fi.FullName.Remove(0, fi.FullName.LastIndexOf("\\") + 1), Environment.NewLine, dllVersion, Environment.NewLine, fi.FullName));
}
}
dlls.Sort();
return dlls;
}
private string CreateDllInfo()
{
string dllInfo = "";
if (radioButtonAll.Checked)
{
foreach (var item in GetAllDllNames())
{
dllInfo = String.Concat(dllInfo, item, Environment.NewLine, Environment.NewLine);
}
}
else
{
foreach (var item in GetInternalDllNames())
{
dllInfo = String.Concat(dllInfo, item, Environment.NewLine, Environment.NewLine);
}
}
while (dllInfo.EndsWith("\n") || dllInfo.EndsWith("\r"))
{
dllInfo = dllInfo.Remove(dllInfo.Length - 1);
}
return dllInfo;
}
private void SetWindow(int width, int hight)
{
//Maximalabmaße prüfen
if (width < 500)
{
width = 500;
}
if (width > 800)
{
width = 800;
}
if (hight < 400)
{
hight = 400;
}
if (hight > 600)
{
hight = 600;
}
//Fenster setzen
this.Size = new Size(width, hight);
this.MaximumSize = new Size(width, hight);
this.MinimumSize = new Size(width, hight);
}
private void Appearance()
{
//Sind im Startup Verzeichnis keine eigenen DLLs, dann ist die EXE und die DLLs wahrscheinlich gepackt. Den Button 'DLL-Info' ausblenden, weil es sonst zu Fehler kommt
bool dllExist = false;
DirectoryInfo ParentDirectory = new DirectoryInfo(Application.StartupPath);
foreach (FileInfo fi in ParentDirectory.GetFiles())
{
if (fi.Extension.ToLower() == ".dll")
{
dllExist = true;
}
}
buttonDllInfo.Enabled = dllExist;
buttonDllInfo.Visible = dllExist;
}
}
}