1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/SharpLibEar/ActionLaunchApp.cs Thu Aug 18 14:35:50 2016 +0200
1.3 @@ -0,0 +1,88 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Diagnostics;
1.7 +using System.IO;
1.8 +using System.Linq;
1.9 +using System.Runtime.Serialization;
1.10 +using System.Text;
1.11 +using System.Threading.Tasks;
1.12 +
1.13 +namespace SharpLib.Ear
1.14 +{
1.15 + [DataContract]
1.16 + [AttributeObject(Id = "Action.Launch.App", Name = "Launch application", Description = "Launch an application.")]
1.17 + public class ActionLaunchApp : Action
1.18 + {
1.19 + [DataMember]
1.20 + [AttributeObjectProperty
1.21 + (
1.22 + Id = "Action.Launch.App.File",
1.23 + Name = "File to launch",
1.24 + Description = "Specifies the application file to launch.",
1.25 + Filter = "EXE files (*.exe)|*.exe"
1.26 + )
1.27 + ]
1.28 + public PropertyFile File { get; set; } = new PropertyFile();
1.29 +
1.30 + [DataMember]
1.31 + [AttributeObjectProperty
1.32 + (
1.33 + Id = "Action.Launch.App.SwitchTo",
1.34 + Name = "Switch to",
1.35 + Description = "Specifies if we should switch the application if it's already launched."
1.36 + )
1.37 + ]
1.38 + public bool SwitchTo { get; set; } = true;
1.39 +
1.40 + [DataMember]
1.41 + [AttributeObjectProperty
1.42 + (
1.43 + Id = "Action.Launch.App.MultipleInstance",
1.44 + Name = "Multiple Instance",
1.45 + Description = "Specifies if We should launch multiple instance."
1.46 + )
1.47 + ]
1.48 + public bool MultipleInstance { get; set; } = false;
1.49 +
1.50 + public override string Brief()
1.51 + {
1.52 + return Name + ": " + Path.GetFileName(File.FullPath);
1.53 + }
1.54 +
1.55 + [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")]
1.56 + public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown);
1.57 +
1.58 +
1.59 + protected override void DoExecute()
1.60 + {
1.61 + //First check if the process we want to launch already exists
1.62 + string procName = Path.GetFileNameWithoutExtension(File.FullPath);
1.63 + Process[] existingProcesses = Process.GetProcessesByName(procName);
1.64 + if (existingProcesses == null || existingProcesses.Length == 0 || MultipleInstance)
1.65 + {
1.66 + // Process do not exists just try to launch it
1.67 + ProcessStartInfo start = new ProcessStartInfo();
1.68 + // Enter in the command line arguments, everything you would enter after the executable name itself
1.69 + //start.Arguments = arguments;
1.70 + // Enter the executable to run, including the complete path
1.71 + start.FileName = File.FullPath;
1.72 + start.WindowStyle = ProcessWindowStyle.Normal;
1.73 + start.CreateNoWindow = true;
1.74 + start.UseShellExecute = true;
1.75 + // Run the external process & wait for it to finish
1.76 + Process proc = Process.Start(start);
1.77 +
1.78 + //SL: We could have used that too
1.79 + //Shell32.Shell shell = new Shell32.Shell();
1.80 + //shell.ShellExecute(Properties.Settings.Default.StartFileName);
1.81 + }
1.82 + else if (SwitchTo)
1.83 + {
1.84 + //This won't work properly until we have a manifest that enables uiAccess.
1.85 + //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
1.86 + SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
1.87 + }
1.88 +
1.89 + }
1.90 + }
1.91 +}