From 9bc26dc611cd570cf5c710714bda4cfc8b1f7ce3 Mon Sep 17 00:00:00 2001 From: Hoeglinger Eugen Date: Fri, 3 Jun 2022 11:07:03 +0200 Subject: [PATCH] Neu dazu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Abfrage ob ein Prozess läuft --- StartStopExtProgram/StartStopExtProgram.cs | 858 +++++++++++---------- 1 file changed, 432 insertions(+), 426 deletions(-) diff --git a/StartStopExtProgram/StartStopExtProgram.cs b/StartStopExtProgram/StartStopExtProgram.cs index 5d65149..579a37c 100644 --- a/StartStopExtProgram/StartStopExtProgram.cs +++ b/StartStopExtProgram/StartStopExtProgram.cs @@ -1,426 +1,432 @@ -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.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace Eugen.ESystem -{ - public class StartStopExtProgram - { - #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 - /// - /// Contains the name of the program file - /// - public static string DllName { set; get; } - - /// - /// Contains the version of the program file - /// - public static string DllVersion { set; get; } - - /// - /// Contains the copyright notice - /// - public static string Copyright { set; get; } - - private static void SetDllInfo() - { - //Name, Version und Copyright setzen - DllName = dllName; - DllVersion = dllVersion; - Copyright = copyright; - } - #endregion - - /// - /// Starts or stops an external program. - /// - /// A valid program name with path and extension. - static StartStopExtProgram() - { - SetDllInfo(); //Name, Version und Copyright der DLL setzen - } - - /// - /// Starts or stops an external program. - /// - /// A valid program name with path and extension. - public StartStopExtProgram(string program) - { - SetDllInfo(); //Name, Version und Copyright der DLL setzen - - SetBaseValues(program); - StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); - } - - /// - /// Starts or stops an external program in declared window style. - /// - /// A valid program name with path and extension. - /// Window style: false=visible, true=minimized. - public StartStopExtProgram(string program, bool winStyleMinimized) - { - SetDllInfo(); //Name, Version und Copyright der DLL setzen - - SetBaseValues(program, winStyleMinimized); - StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); - } - - /// - /// Starts or stops an external program in declared window style and transfer parameter. - /// - /// A valid program name with path and extension. - /// A valid transfer parameter. - /// Window style: false=visible, true=minimized. - public StartStopExtProgram(string program, string arg, bool winStyleMinimized) - { - SetDllInfo(); //Name, Version und Copyright der DLL setzen - - SetBaseValues(program, arg, winStyleMinimized); - StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); - } - - /// - /// Starts or stops an external program in declared window style and transfer parameter. - /// - /// A valid program path. - /// A valid program name with extension. - /// A valid transfer parameter. - /// Window style: false=visible, true=minimized. - public StartStopExtProgram(string programPath, string programName, string arg, bool winStyleMinimized) - { - SetDllInfo(); //Name, Version und Copyright der DLL setzen - - SetBaseValues(programPath, programName, arg, winStyleMinimized); - StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); - } - - protected string ProgramPath { set; get; } - protected string Program { set; get; } - protected string Arg { set; get; } - protected bool WinStyleMinimized { set; get; } - protected string ProcessName { set; get; } - - private void SetBaseValues(string program) - { - if (File.Exists(program)) - { - Program = Path.GetFileName(program); - ProgramPath = Path.GetDirectoryName(program); - } - else - { - throw new NoValidProgramNameException(Language.message001); - } - - Arg = ""; - WinStyleMinimized = false; - - ProcessName = SetProcessName(program); - } - - private void SetBaseValues(string program, bool winStyleMinimized) - { - if (File.Exists(program)) - { - Program = Path.GetFileName(program); - ProgramPath = Path.GetDirectoryName(program); - } - else - { - throw new NoValidProgramNameException(Language.message001); - } - - Arg = ""; - WinStyleMinimized = winStyleMinimized; - - ProcessName = SetProcessName(program); - } - - private void SetBaseValues(string program, string arg, bool winStyleMinimized) - { - if (File.Exists(program)) - { - Program = Path.GetFileName(program); - ProgramPath = Path.GetDirectoryName(program); - } - else - { - throw new NoValidProgramNameException(Language.message001); - } - - Arg = arg; - WinStyleMinimized = winStyleMinimized; - - ProcessName = SetProcessName(program); - } - - private void SetBaseValues(string programPath, string programName, string arg, bool winStyleMinimized) - { - if (Directory.Exists(programPath)) - { - ProgramPath = programPath; - } - else - { - throw new NoValidFolderStructureException(Language.message003); - } - - if (File.Exists(String.Concat(programPath, "\\", programName))) - { - Program = programName; - } - else - { - throw new NoValidProgramNameException(Language.message002); - } - - Arg = arg; - WinStyleMinimized = winStyleMinimized; - - ProcessName = SetProcessName(programName); - } - - private string SetProcessName(string program) - { - if (!String.IsNullOrEmpty(program)) - { - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - sb.Append(program.Substring(0, 1).ToUpper() + program.Substring(1)); - return Path.GetFileNameWithoutExtension(sb.ToString()); - } - else - { - throw new UnableToGenerateProcessNameException(Language.message002); - } - } - - private void StartStop(string program, string arg, bool winStyle, string processName) - { - if (!CheckProcessIsRun(processName)) - { - Start(program, arg, winStyle); - } - else - { - Stop(program); - } - } - - private static bool CheckProcessIsRun(string processName) - { - //Prüfen ob der Prozess läuft - return (Process.GetProcessesByName(processName).Length > 0); - } - - private void Start(string program, string arg, bool winStyle) - { - try - { - ProcessStartInfo startProgram = new ProcessStartInfo(program); - startProgram.Arguments = arg; - if (winStyle) - { - startProgram.WindowStyle = ProcessWindowStyle.Minimized; - } - Process.Start(startProgram); - } - catch (Exception) - { - throw new UnableToRunProgramException(Language.message005); - } - } - - private void Stop(string program) - { - try - { - string process = program.Remove(0, program.LastIndexOf(@"\") + 1); - process = process.Remove(process.Length - 4); - - Process[] close = Process.GetProcessesByName(process); - close[0].CloseMainWindow(); - close[0].Kill(); - } - catch (Exception ex) - { - throw new UnableToStopProgramException("Programm konnte nicht beendet werden!\n\n" + ex); - } - } - } - - #region Exceptions - class NoValidFolderStructureException : ApplicationException - { - public NoValidFolderStructureException() - { - } - - public NoValidFolderStructureException(string message) - : base(message) - { - } - - public NoValidFolderStructureException(string message, Exception inner) - : base(message, inner) - { - } - } - - class NoValidProgramNameException : ApplicationException - { - public NoValidProgramNameException() - { - } - - public NoValidProgramNameException(string message) - : base(message) - { - } - - public NoValidProgramNameException(string message, Exception inner) - : base(message, inner) - { - } - } - - class UnknownErrorException : ApplicationException - { - public UnknownErrorException() - { - } - - public UnknownErrorException(string message) - : base(message) - { - } - - public UnknownErrorException(string message, Exception inner) - : base(message, inner) - { - } - } - - class UnableToRunProgramException : ApplicationException - { - public UnableToRunProgramException() - { - } - - public UnableToRunProgramException(string message) - : base(message) - { - } - - public UnableToRunProgramException(string message, Exception inner) - : base(message, inner) - { - } - } - - class UnableToStopProgramException : ApplicationException - { - public UnableToStopProgramException() - { - } - - public UnableToStopProgramException(string message) - : base(message) - { - } - - public UnableToStopProgramException(string message, Exception inner) - : base(message, inner) - { - } - } - - class UnableToGenerateProcessNameException : ApplicationException - { - public UnableToGenerateProcessNameException() - { - } - - public UnableToGenerateProcessNameException(string message) - : base(message) - { - } - - public UnableToGenerateProcessNameException(string message, Exception inner) - : base(message, inner) - { - } - } - #endregion -} +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.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Eugen.ESystem +{ + public class StartStopExtProgram + { + #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 + /// + /// Contains the name of the program file + /// + public static string DllName { set; get; } + + /// + /// Contains the version of the program file + /// + public static string DllVersion { set; get; } + + /// + /// Contains the copyright notice + /// + public static string Copyright { set; get; } + + private static void SetDllInfo() + { + //Name, Version und Copyright setzen + DllName = dllName; + DllVersion = dllVersion; + Copyright = copyright; + } + #endregion + + /// + /// Starts or stops an external program. + /// + /// A valid program name with path and extension. + static StartStopExtProgram() + { + SetDllInfo(); //Name, Version und Copyright der DLL setzen + } + + /// + /// Starts or stops an external program. + /// + /// A valid program name with path and extension. + public StartStopExtProgram(string program) + { + SetDllInfo(); //Name, Version und Copyright der DLL setzen + + SetBaseValues(program); + StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); + } + + /// + /// Starts or stops an external program in declared window style. + /// + /// A valid program name with path and extension. + /// Window style: false=visible, true=minimized. + public StartStopExtProgram(string program, bool winStyleMinimized) + { + SetDllInfo(); //Name, Version und Copyright der DLL setzen + + SetBaseValues(program, winStyleMinimized); + StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); + } + + /// + /// Starts or stops an external program in declared window style and transfer parameter. + /// + /// A valid program name with path and extension. + /// A valid transfer parameter. + /// Window style: false=visible, true=minimized. + public StartStopExtProgram(string program, string arg, bool winStyleMinimized) + { + SetDllInfo(); //Name, Version und Copyright der DLL setzen + + SetBaseValues(program, arg, winStyleMinimized); + StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); + } + + /// + /// Starts or stops an external program in declared window style and transfer parameter. + /// + /// A valid program path. + /// A valid program name with extension. + /// A valid transfer parameter. + /// Window style: false=visible, true=minimized. + public StartStopExtProgram(string programPath, string programName, string arg, bool winStyleMinimized) + { + SetDllInfo(); //Name, Version und Copyright der DLL setzen + + SetBaseValues(programPath, programName, arg, winStyleMinimized); + StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName); + } + + protected string ProgramPath { set; get; } + protected string Program { set; get; } + protected string Arg { set; get; } + protected bool WinStyleMinimized { set; get; } + protected string ProcessName { set; get; } + + private void SetBaseValues(string program) + { + if (File.Exists(program)) + { + Program = Path.GetFileName(program); + ProgramPath = Path.GetDirectoryName(program); + } + else + { + throw new NoValidProgramNameException(Language.message001); + } + + Arg = ""; + WinStyleMinimized = false; + + ProcessName = SetProcessName(program); + } + + private void SetBaseValues(string program, bool winStyleMinimized) + { + if (File.Exists(program)) + { + Program = Path.GetFileName(program); + ProgramPath = Path.GetDirectoryName(program); + } + else + { + throw new NoValidProgramNameException(Language.message001); + } + + Arg = ""; + WinStyleMinimized = winStyleMinimized; + + ProcessName = SetProcessName(program); + } + + private void SetBaseValues(string program, string arg, bool winStyleMinimized) + { + if (File.Exists(program)) + { + Program = Path.GetFileName(program); + ProgramPath = Path.GetDirectoryName(program); + } + else + { + throw new NoValidProgramNameException(Language.message001); + } + + Arg = arg; + WinStyleMinimized = winStyleMinimized; + + ProcessName = SetProcessName(program); + } + + private void SetBaseValues(string programPath, string programName, string arg, bool winStyleMinimized) + { + if (Directory.Exists(programPath)) + { + ProgramPath = programPath; + } + else + { + throw new NoValidFolderStructureException(Language.message003); + } + + if (File.Exists(String.Concat(programPath, "\\", programName))) + { + Program = programName; + } + else + { + throw new NoValidProgramNameException(Language.message002); + } + + Arg = arg; + WinStyleMinimized = winStyleMinimized; + + ProcessName = SetProcessName(programName); + } + + private string SetProcessName(string program) + { + if (!String.IsNullOrEmpty(program)) + { + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + sb.Append(program.Substring(0, 1).ToUpper() + program.Substring(1)); + return Path.GetFileNameWithoutExtension(sb.ToString()); + } + else + { + throw new UnableToGenerateProcessNameException(Language.message002); + } + } + + private void StartStop(string program, string arg, bool winStyle, string processName) + { + if (!CheckProcessIsRun(processName)) + { + Start(program, arg, winStyle); + } + else + { + Stop(program); + } + } + + private static bool CheckProcessIsRun(string processName) + { + //Prüfen ob der Prozess läuft + return (Process.GetProcessesByName(processName).Length > 0); + } + + public bool CheckProcessIsRun() + { + //Prüfen ob der Prozess läuft + return (Process.GetProcessesByName(ProcessName).Length > 0); + } + + private void Start(string program, string arg, bool winStyle) + { + try + { + ProcessStartInfo startProgram = new ProcessStartInfo(program); + startProgram.Arguments = arg; + if (winStyle) + { + startProgram.WindowStyle = ProcessWindowStyle.Minimized; + } + Process.Start(startProgram); + } + catch (Exception) + { + throw new UnableToRunProgramException(Language.message005); + } + } + + private void Stop(string program) + { + try + { + string process = program.Remove(0, program.LastIndexOf(@"\") + 1); + process = process.Remove(process.Length - 4); + + Process[] close = Process.GetProcessesByName(process); + close[0].CloseMainWindow(); + close[0].Kill(); + } + catch (Exception ex) + { + throw new UnableToStopProgramException("Programm konnte nicht beendet werden!\n\n" + ex); + } + } + } + + #region Exceptions + class NoValidFolderStructureException : ApplicationException + { + public NoValidFolderStructureException() + { + } + + public NoValidFolderStructureException(string message) + : base(message) + { + } + + public NoValidFolderStructureException(string message, Exception inner) + : base(message, inner) + { + } + } + + class NoValidProgramNameException : ApplicationException + { + public NoValidProgramNameException() + { + } + + public NoValidProgramNameException(string message) + : base(message) + { + } + + public NoValidProgramNameException(string message, Exception inner) + : base(message, inner) + { + } + } + + class UnknownErrorException : ApplicationException + { + public UnknownErrorException() + { + } + + public UnknownErrorException(string message) + : base(message) + { + } + + public UnknownErrorException(string message, Exception inner) + : base(message, inner) + { + } + } + + class UnableToRunProgramException : ApplicationException + { + public UnableToRunProgramException() + { + } + + public UnableToRunProgramException(string message) + : base(message) + { + } + + public UnableToRunProgramException(string message, Exception inner) + : base(message, inner) + { + } + } + + class UnableToStopProgramException : ApplicationException + { + public UnableToStopProgramException() + { + } + + public UnableToStopProgramException(string message) + : base(message) + { + } + + public UnableToStopProgramException(string message, Exception inner) + : base(message, inner) + { + } + } + + class UnableToGenerateProcessNameException : ApplicationException + { + public UnableToGenerateProcessNameException() + { + } + + public UnableToGenerateProcessNameException(string message) + : base(message) + { + } + + public UnableToGenerateProcessNameException(string message, Exception inner) + : base(message, inner) + { + } + } + #endregion +}