Files
GFATask/GFATask/Classes/AppExecution.cs

95 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GFATask
{
/// <summary>
/// Beinhaltet Methoden wie und welche App gestartet wird
/// </summary>
public class AppExecution
{
/// <summary>
/// Startet die App vom bestimmten Pfad mit den gewünschten Argumenten
/// </summary>
/// <param name="command">Der auszuführende Befehl zum starten der App</param>
/// <param name="args">Argumente mit der die App ausgeführt werden soll</param>
public static bool FromApp(string name, string command, string args)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
try
{
// Handelt es sich weder um eine App Verlinkung, noch um eine SystemApp, wird die Ausführung normal durchgeführt
if (!IsAppLink(name, command) && !StartSystemApp(command))
{
Process app = new Process();
app.StartInfo.WorkingDirectory = EnvironmentManager.VariableConverter(Path.GetDirectoryName(command) == null ? command : Path.GetDirectoryName(command), true);
app.StartInfo.FileName = EnvironmentManager.VariableConverter(command, true);
app.StartInfo.Arguments = args;
app.Start();
stopwatch.Stop();
return true;
}
return false;
}
catch (Exception ex)
{
stopwatch.Stop();
Log.EventLog.Write(string.Format("Start der App {0} nach {1}ms fehlgeschlagen", name, (stopwatch.ElapsedMilliseconds).ToString()), LogClassification.Warning, ex);
stopwatch = null;
return false;
}
}
/// <summary>
/// Prüft, ob es sich um eine verlinkte App handelt und führt den Befehl dieser App aus
/// </summary>
/// <returns></returns>
private static bool IsAppLink(string name, string command)
{
if (command.StartsWith("APPLNK:"))
{
Models.App applnk = Cast.ToAppLink(command).ToApp();
foreach (var exec in applnk.Execute)
if (FromApp(name, exec.Command, exec.Args))
break;
return true;
}
else
{
return false;
}
}
private static bool StartSystemApp(string name)
{
bool trueConditions = false;
switch (name)
{
case "kill":
SystemApps.Kill(out trueConditions);
break;
case "settings":
SystemApps.Settings(out trueConditions);
break;
case "adm":
SystemApps.Administration(out trueConditions);
break;
case "info":
SystemApps.Info(out trueConditions);
break;
}
return trueConditions;
}
}
}