private static Process ExecuteCommand(string pWorkDir, string pExecutable, string pArguments, string pProcessName,
bool pShowInstanceWindow, string pIconFile, bool pProcessNameViaShortcut, AemInstance pAemInstance)
{
mLog.Info("Execute: WorkDir=" + pWorkDir + ", executable=" + pExecutable + ", arguments=" + pArguments);
Process process;
// execute via auto-generated shortcut
if (pShowInstanceWindow && pProcessNameViaShortcut) {
string shortcutFilename = Path.GetTempPath() + pProcessName + ".lnk";
IWshRuntimeLibrary.WshShell wshShell = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(shortcutFilename);
if (!string.IsNullOrEmpty(pWorkDir)) {
shortcut.WorkingDirectory = pWorkDir;
}
try {
shortcut.TargetPath = pExecutable;
}
catch (ArgumentException) {
MessageBox.Show("Executable not found: " + pExecutable, "Execute Command", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return null;
}
shortcut.Arguments = pArguments;
shortcut.Description = pProcessName;
if (!string.IsNullOrEmpty(pIconFile)) {
shortcut.IconLocation = Path.GetDirectoryName(Application.ExecutablePath) + "\\icons\\" + pIconFile;
}
shortcut.Save();
process = new Process();
process.StartInfo.FileName = shortcutFilename;
}
// start directly
else {
process = new Process();
if (!string.IsNullOrEmpty(pWorkDir)) {
process.StartInfo.WorkingDirectory = pWorkDir;
}
process.StartInfo.FileName = pExecutable;
process.StartInfo.Arguments = pArguments;
}
if (!pShowInstanceWindow) {
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
if (pProcessNameViaShortcut) {
// use shellexecute if start via shortcut is used - this forbids using output stream redirection etc.
process.StartInfo.UseShellExecute = true;
}
else {
// directy start process if no shortcut is used.
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = !pShowInstanceWindow;
if (pAemInstance != null && !pShowInstanceWindow) {
// use output handling if AEM instance is available
process.OutputDataReceived += new DataReceivedEventHandler(pAemInstance.ConsoleOutputWindow.Process_OutputDataReceived);
process.StartInfo.RedirectStandardOutput = true;
process.ErrorDataReceived += new DataReceivedEventHandler(pAemInstance.ConsoleOutputWindow.Process_ErrorDataReceived);
process.StartInfo.RedirectStandardError = true;
}
}
try {
process.Start();
if (!pProcessNameViaShortcut) {
if (pAemInstance != null && !pShowInstanceWindow) {
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}
}
return process;
}
catch (Exception ex) {
throw new Exception(ex.Message + "\n"
+ "WorkDir: " + pWorkDir + "\n"
+ "Executable: " + pExecutable + "\n"
+ "Arguments: " + pArguments, ex);
}
}