Neu dazu
- Abfrage ob ein Prozess läuft
This commit is contained in:
parent
290c16d329
commit
9bc26dc611
@ -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
|
||||
/// <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>
|
||||
/// Starts or stops an external program.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
static StartStopExtProgram()
|
||||
{
|
||||
SetDllInfo(); //Name, Version und Copyright der DLL setzen
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
public StartStopExtProgram(string program)
|
||||
{
|
||||
SetDllInfo(); //Name, Version und Copyright der DLL setzen
|
||||
|
||||
SetBaseValues(program);
|
||||
StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program in declared window style.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
/// <param name="winStyleMinimized">Window style: false=visible, true=minimized.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program in declared window style and transfer parameter.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
/// <param name="arg">A valid transfer parameter.</param>
|
||||
/// <param name="winStyleMinimized">Window style: false=visible, true=minimized.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program in declared window style and transfer parameter.
|
||||
/// </summary>
|
||||
/// <param name="programPath">A valid program path.</param>
|
||||
/// <param name="programName">A valid program name with extension.</param>
|
||||
/// <param name="arg">A valid transfer parameter.</param>
|
||||
/// <param name="winStyleMinimized">Window style: false=visible, true=minimized.</param>
|
||||
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
|
||||
/// <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>
|
||||
/// Starts or stops an external program.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
static StartStopExtProgram()
|
||||
{
|
||||
SetDllInfo(); //Name, Version und Copyright der DLL setzen
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
public StartStopExtProgram(string program)
|
||||
{
|
||||
SetDllInfo(); //Name, Version und Copyright der DLL setzen
|
||||
|
||||
SetBaseValues(program);
|
||||
StartStop(String.Concat(ProgramPath, "\\", Program), Arg, WinStyleMinimized, ProcessName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program in declared window style.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
/// <param name="winStyleMinimized">Window style: false=visible, true=minimized.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program in declared window style and transfer parameter.
|
||||
/// </summary>
|
||||
/// <param name="program">A valid program name with path and extension.</param>
|
||||
/// <param name="arg">A valid transfer parameter.</param>
|
||||
/// <param name="winStyleMinimized">Window style: false=visible, true=minimized.</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts or stops an external program in declared window style and transfer parameter.
|
||||
/// </summary>
|
||||
/// <param name="programPath">A valid program path.</param>
|
||||
/// <param name="programName">A valid program name with extension.</param>
|
||||
/// <param name="arg">A valid transfer parameter.</param>
|
||||
/// <param name="winStyleMinimized">Window style: false=visible, true=minimized.</param>
|
||||
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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user