openhelpfile_dll_visualstudio/OpenHelpFile/OpenHelpFile.cs
2020-11-25 17:41:11 +01:00

297 lines
10 KiB
C#

using Eugen.ESystem.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DisplLanguage = Eugen.ESystem.Windows.Forms.Languages.Language;
namespace Eugen.ESystem
{
/// <summary>
/// Opens the help file in the given language.
/// </summary>
public class OpenHelpFile
{
#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 program file
/// </summary>
public static string DllName { set; get; }
/// <summary>
/// Contains the version of the program file
/// </summary>
public static string DllVersion { set; get; }
/// <summary>
/// Contains the copyright notice
/// </summary>
public static string Copyright { set; get; }
private static void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
}
#endregion
/// <summary>
/// Opens the help file in the given language.
/// </summary>
static OpenHelpFile()
{
SetDllInfo();
}
/// <summary>
/// Opens the help file in the desired language
/// </summary>
/// <param name="language">A valid language. E.G.:'german' or 'english'</param>
public static void Open(string language)
{
//Systemsprache aus alle möglichen Sprachen ermitteln
DisplLanguage dispLanguage = DisplLanguage.German;
foreach (var e in Enum.GetValues(typeof(DisplLanguage)))
{
if (language.ToLower() == e.ToString().ToLower())
{
dispLanguage = (Languages.Language)e;
}
}
//Prüfen ob die Hilfe schon läuft. Wenn nicht dann aufrufen
if (!CheckIfRun())
{
//Hilfedatei setzen
string helpFile = SetHelpFile(dispLanguage);
//Hilfedatei ins Temp-Verzeichnis kopieren, damit der Inhalt von Windows angezeigt wird
helpFile = CopyHelpFileToTemp(helpFile);
//Hilfedatei aufrufen
ShowHelpFile(helpFile);
}
}
private static bool CheckIfRun()
{
string program = "hh";
bool run = false;
Process[] ProgrammTask = Process.GetProcesses();
foreach (Process ScannTask in ProgrammTask)
{
if (ScannTask.ProcessName == program)
{
run = true;
}
}
return run;
}
private static string SetHelpFile(DisplLanguage dispLanguage)
{
string helpFile = "";
switch (dispLanguage.ToString())
{
case "German":
helpFile = String.Concat(Application.StartupPath, "\\Hilfe.chm");
break;
case "English":
helpFile = String.Concat(Application.StartupPath, "\\Help.chm");
break;
case "French":
helpFile = String.Concat(Application.StartupPath, "\\Aide.chm");
break;
case "Italian":
helpFile = String.Concat(Application.StartupPath, "\\Aiuto.chm");
break;
case "Spanish":
helpFile = String.Concat(Application.StartupPath, "\\Ayuda.chm");
break;
case "Portuguese":
helpFile = String.Concat(Application.StartupPath, "\\Ajuda.chm");
break;
case "Finska":
helpFile = String.Concat(Application.StartupPath, "\\Auta.chm");
break;
case "Swedish":
helpFile = String.Concat(Application.StartupPath, "\\Hjälp.chm");
break;
case "Czech":
helpFile = String.Concat(Application.StartupPath, "\\Pomoc.chm");
break;
case "Slovak":
helpFile = String.Concat(Application.StartupPath, "\\Pomoc.chm");
break;
case "Slovenian":
helpFile = String.Concat(Application.StartupPath, "\\Pomoč.chm");
break;
case "Polish":
helpFile = String.Concat(Application.StartupPath, "\\Pomoc_.chm");
break;
case "Bulgarian":
helpFile = String.Concat(Application.StartupPath, "\\Помогне.chm");
break;
case "Romanian":
helpFile = String.Concat(Application.StartupPath, "\\Ajutor.chm");
break;
case "Hungarian":
helpFile = String.Concat(Application.StartupPath, "\\Segítség.chm");
break;
case "Russian":
helpFile = String.Concat(Application.StartupPath, "\\Помощь.chm");
break;
case "Japanese":
helpFile = String.Concat(Application.StartupPath, "\\ヘルプ.chm");
break;
case "Chinese":
helpFile = String.Concat(Application.StartupPath, "\\帮忙.chm");
break;
default:
break;
}
return helpFile;
}
private static string CopyHelpFileToTemp(string helpfile)
{
string file = Path.GetFileName(helpfile);
string tempHelpFile = String.Concat("C:\\Temp\\", file);
try
{
//Vorhandene Datei löschen
if (File.Exists(tempHelpFile))
{
File.Delete(tempHelpFile);
}
//Hilfedatei in Temp-Verzeichnis kopieren
if (!Directory.Exists("C:\\Temp"))
{
Directory.CreateDirectory("C:\\Temp");
}
File.Copy(helpfile, tempHelpFile);
//Wenn das Kopieren erfolgreich war, die Datei mit neuem Pfad zurückgeben
if (File.Exists(tempHelpFile))
{
return tempHelpFile; //Wenn die Datei existiert, dann die temporäre Hilfedatei zurückgeben
}
else
{
return helpfile; //Wenn die Datei nicht existiert, dann die ursprüngliche Hilfedatei zurückgeben
}
}
catch
{
return helpfile; //Wenn ein Fehler passiert ist, dann die ursprüngliche Hilfedatei zurückgeben
}
}
private static void ShowHelpFile(string helpFile)
{
if (!File.Exists(helpFile))
{
throw new FileNotFoundException(helpFile);
}
try
{
Process.Start(helpFile);
}
catch (CannotShowHelpFileException)
{
throw new CannotShowHelpFileException(helpFile);
}
}
}
#region Exception
public class CannotShowHelpFileException : ApplicationException
{
public CannotShowHelpFileException()
{ }
public CannotShowHelpFileException(string message) : base(message)
{ }
public CannotShowHelpFileException(string message, Exception inner) : base(message, inner)
{ }
}
#endregion
}