Neuerstellung
This commit is contained in:
313
FSI.BT.Tools/Commands/OpenAppCommand.cs
Normal file
313
FSI.BT.Tools/Commands/OpenAppCommand.cs
Normal file
@@ -0,0 +1,313 @@
|
||||
using FSI.Lib;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
namespace FSI.BT.Tools.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Shows the main window.
|
||||
/// </summary>
|
||||
public class OpenAppCommand : CommandBase<OpenAppCommand>
|
||||
{
|
||||
public override void Execute(object parameter)
|
||||
{
|
||||
string[] files = new string[] { };
|
||||
string[] pathes = new string[] { };
|
||||
string arguments = string.Empty;
|
||||
|
||||
switch ((string)parameter)
|
||||
{
|
||||
case "SimaticManager":
|
||||
files = Global.Settings.Apps.SieSimaticManagerExe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv13":
|
||||
files = Global.Settings.Apps.SieTiaV13Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv14":
|
||||
files = Global.Settings.Apps.SieTiaV14Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv15":
|
||||
files = Global.Settings.Apps.SieTiaV15Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv16":
|
||||
files = Global.Settings.Apps.SieTiaV16Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv17":
|
||||
files = Global.Settings.Apps.SieTiaV17Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "Starter":
|
||||
files = Global.Settings.Apps.SieTiaVStarterExe.Split(";");
|
||||
break;
|
||||
|
||||
case "Epl":
|
||||
files = Global.Settings.Apps.Epl.Exe.Split(";");
|
||||
arguments = Global.Settings.Apps.Epl.Arguments;
|
||||
break;
|
||||
|
||||
case "EplPrj":
|
||||
Lib.Guis.Prj.Mgt.FrmMain frmMainEplPrj = new()
|
||||
{
|
||||
ShowPdf = false,
|
||||
CloseAtLostFocus = true,
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner,
|
||||
};
|
||||
frmMainEplPrj.Show();
|
||||
return;
|
||||
|
||||
case "EplPdf":
|
||||
Lib.Guis.Prj.Mgt.FrmMain frmMainEplPdf = new()
|
||||
{
|
||||
ShowPdf = true,
|
||||
CloseAtLostFocus = true,
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner,
|
||||
};
|
||||
frmMainEplPdf.Show();
|
||||
return;
|
||||
|
||||
case "EplPdfMgt":
|
||||
Lib.Guis.Pdf.Mgt.FrmMain frmMainEplPdfMgt = new()
|
||||
{
|
||||
CloseAtLostFocus = true
|
||||
};
|
||||
frmMainEplPdfMgt.Show();
|
||||
return;
|
||||
|
||||
case "Npp":
|
||||
files = Global.Settings.Apps.NppExe.Split(";");
|
||||
break;
|
||||
|
||||
case "TotalCmd":
|
||||
files = Global.Settings.Apps.TotalCmdExe.Split(";");
|
||||
break;
|
||||
|
||||
case "TeXstudio":
|
||||
files = Global.Settings.Apps.TeXstudioExe.Split(";");
|
||||
pathes = Global.Settings.Apps.TeXstudioPath.Split(";");
|
||||
break;
|
||||
|
||||
case "VS":
|
||||
files = Global.Settings.Apps.VsExe.Split(";");
|
||||
break;
|
||||
|
||||
case "VS.Code":
|
||||
files = Global.Settings.Apps.VsCodeExe.Split(";");
|
||||
break;
|
||||
|
||||
|
||||
case "Rdp":
|
||||
files = Global.Settings.Apps.RdpExe.Split(";"); ;
|
||||
break;
|
||||
|
||||
case "DeEncrypt":
|
||||
Lib.Guis.DeEncryptMessage.FrmMain frmMainDeEnCrypt = new()
|
||||
{
|
||||
Password = Global.Settings.General.DeEnCryptPasswort,
|
||||
CloseAtLostFocus = true,
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner,
|
||||
};
|
||||
frmMainDeEnCrypt.Show();
|
||||
return;
|
||||
|
||||
case "StarterCsvExporter":
|
||||
Lib.Guis.SieStarterCsvExporter.FrmMain frmMain = new();
|
||||
frmMain.Show();
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
string fileName = string.Empty;
|
||||
string path = string.Empty;
|
||||
|
||||
for (int i = 0; i <= files.Length - 1; i++)
|
||||
{
|
||||
if (File.Exists(Environment.ExpandEnvironmentVariables(files[i].Trim())))
|
||||
{
|
||||
fileName = Environment.ExpandEnvironmentVariables(files[i].Trim());
|
||||
}
|
||||
|
||||
if (pathes.Length == 0)
|
||||
{
|
||||
path = Path.GetDirectoryName(fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
path = Environment.ExpandEnvironmentVariables(pathes[i].Trim());
|
||||
}
|
||||
}
|
||||
|
||||
if (ProgramIsRunning(fileName))
|
||||
{
|
||||
ProgramToFront(fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Process process = new();
|
||||
process.StartInfo.FileName = fileName;
|
||||
process.StartInfo.WorkingDirectory = path;
|
||||
process.StartInfo.Arguments = arguments;
|
||||
|
||||
try
|
||||
{
|
||||
process.Start();
|
||||
}
|
||||
catch (System.ComponentModel.Win32Exception ex) when (ex.NativeErrorCode == 740)
|
||||
{
|
||||
try
|
||||
{
|
||||
process.StartInfo.UseShellExecute = true;
|
||||
process.StartInfo.Verb = "runas";
|
||||
process.Start();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanExecute(object parameter)
|
||||
{
|
||||
string[] files = new string[] { };
|
||||
switch ((string)parameter)
|
||||
{
|
||||
case "SimaticManager":
|
||||
files = Global.Settings.Apps.SieSimaticManagerExe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv13":
|
||||
files = Global.Settings.Apps.SieTiaV13Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv14":
|
||||
files = Global.Settings.Apps.SieTiaV14Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv15":
|
||||
files = Global.Settings.Apps.SieTiaV15Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv16":
|
||||
files = Global.Settings.Apps.SieTiaV16Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "TIAv17":
|
||||
files = Global.Settings.Apps.SieTiaV17Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "Starter":
|
||||
files = Global.Settings.Apps.SieTiaVStarterExe.Split(";");
|
||||
break;
|
||||
|
||||
case "Epl":
|
||||
files = Global.Settings.Apps.Epl.Exe.Split(";");
|
||||
break;
|
||||
|
||||
case "EplPrj":
|
||||
return true;
|
||||
|
||||
case "EplPdf":
|
||||
return true;
|
||||
|
||||
case "EplPdfMgt":
|
||||
return Global.AdminRights;
|
||||
|
||||
case "Npp":
|
||||
files = Global.Settings.Apps.NppExe.Split(";");
|
||||
break;
|
||||
|
||||
case "TotalCmd":
|
||||
files = Global.Settings.Apps.TotalCmdExe.Split(";");
|
||||
break;
|
||||
|
||||
case "TeXstudio":
|
||||
files = Global.Settings.Apps.TeXstudioExe.Split(";");
|
||||
break;
|
||||
|
||||
case "VS":
|
||||
files = Global.Settings.Apps.VsExe.Split(";");
|
||||
break;
|
||||
|
||||
case "VS.Code":
|
||||
files = Global.Settings.Apps.VsCodeExe.Split(";");
|
||||
break;
|
||||
|
||||
case "Rdp":
|
||||
files = Global.Settings.Apps.RdpExe.Split(";"); ;
|
||||
break;
|
||||
|
||||
case "DeEncrypt":
|
||||
return Global.AdminRights;
|
||||
|
||||
case "StarterCsvExporter":
|
||||
return Global.AdminRights;
|
||||
default: return false;
|
||||
|
||||
}
|
||||
|
||||
foreach (string file in files)
|
||||
{
|
||||
if (File.Exists(Environment.ExpandEnvironmentVariables(file.Trim())))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool ProgramIsRunning(string FullPath)
|
||||
{
|
||||
string FilePath = Path.GetDirectoryName(FullPath);
|
||||
string FileName = Path.GetFileNameWithoutExtension(FullPath).ToLower();
|
||||
bool isRunning = false;
|
||||
|
||||
Process[] pList = Process.GetProcessesByName(FileName);
|
||||
|
||||
foreach (Process p in pList)
|
||||
{
|
||||
if (p.MainModule.FileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
isRunning = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
[System.Runtime.InteropServices.DllImport("User32.dll")]
|
||||
private static extern bool SetForegroundWindow(IntPtr handle);
|
||||
[System.Runtime.InteropServices.DllImport("User32.dll")]
|
||||
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
|
||||
[System.Runtime.InteropServices.DllImport("User32.dll")]
|
||||
private static extern bool IsIconic(IntPtr handle);
|
||||
|
||||
private void ProgramToFront(string FullPath)
|
||||
{
|
||||
string FilePath = Path.GetDirectoryName(FullPath);
|
||||
string FileName = Path.GetFileNameWithoutExtension(FullPath).ToLower();
|
||||
|
||||
Process[] pList = Process.GetProcessesByName(FileName);
|
||||
|
||||
foreach (Process p in pList)
|
||||
{
|
||||
if (p.MainModule.FileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
IntPtr handle = p.MainWindowHandle;
|
||||
if (IsIconic(handle))
|
||||
{
|
||||
ShowWindow(handle, 9);
|
||||
}
|
||||
SetForegroundWindow(handle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user