323 lines
9.6 KiB
C#
323 lines
9.6 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.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Eugen.ESystem
|
|
{
|
|
public class StartStopExtProgram
|
|
{
|
|
/// <summary>
|
|
/// Starts or stops an external program.
|
|
/// </summary>
|
|
/// <param name="program">A valid program name with path and extension.</param>
|
|
public StartStopExtProgram(string program)
|
|
{
|
|
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)
|
|
{
|
|
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)
|
|
{
|
|
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)
|
|
{
|
|
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
|
|
}
|