StephaneLenclud@238: using System; StephaneLenclud@238: using System.Collections.Generic; StephaneLenclud@238: using System.Diagnostics; StephaneLenclud@238: using System.IO; StephaneLenclud@238: using System.Linq; StephaneLenclud@238: using System.Runtime.Serialization; StephaneLenclud@238: using System.Text; StephaneLenclud@238: using System.Threading.Tasks; StephaneLenclud@238: StephaneLenclud@238: namespace SharpLib.Ear StephaneLenclud@238: { StephaneLenclud@238: [DataContract] StephaneLenclud@238: [AttributeObject(Id = "Action.Launch.App", Name = "Launch application", Description = "Launch an application.")] StephaneLenclud@238: public class ActionLaunchApp : Action StephaneLenclud@238: { StephaneLenclud@238: [DataMember] StephaneLenclud@238: [AttributeObjectProperty StephaneLenclud@238: ( StephaneLenclud@238: Id = "Action.Launch.App.File", StephaneLenclud@238: Name = "File to launch", StephaneLenclud@238: Description = "Specifies the application file to launch.", StephaneLenclud@238: Filter = "EXE files (*.exe)|*.exe" StephaneLenclud@238: ) StephaneLenclud@238: ] StephaneLenclud@238: public PropertyFile File { get; set; } = new PropertyFile(); StephaneLenclud@238: StephaneLenclud@238: [DataMember] StephaneLenclud@238: [AttributeObjectProperty StephaneLenclud@238: ( StephaneLenclud@238: Id = "Action.Launch.App.SwitchTo", StephaneLenclud@238: Name = "Switch to", StephaneLenclud@238: Description = "Specifies if we should switch the application if it's already launched." StephaneLenclud@238: ) StephaneLenclud@238: ] StephaneLenclud@238: public bool SwitchTo { get; set; } = true; StephaneLenclud@238: StephaneLenclud@238: [DataMember] StephaneLenclud@238: [AttributeObjectProperty StephaneLenclud@238: ( StephaneLenclud@238: Id = "Action.Launch.App.MultipleInstance", StephaneLenclud@238: Name = "Multiple Instance", StephaneLenclud@238: Description = "Specifies if We should launch multiple instance." StephaneLenclud@238: ) StephaneLenclud@238: ] StephaneLenclud@238: public bool MultipleInstance { get; set; } = false; StephaneLenclud@238: StephaneLenclud@238: public override string Brief() StephaneLenclud@238: { StephaneLenclud@238: return Name + ": " + Path.GetFileName(File.FullPath); StephaneLenclud@238: } StephaneLenclud@238: StephaneLenclud@239: public override bool IsValid() StephaneLenclud@239: { StephaneLenclud@239: // This is a valid configuration only if our file exists StephaneLenclud@239: return System.IO.File.Exists(File.FullPath); StephaneLenclud@239: } StephaneLenclud@239: StephaneLenclud@238: [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")] StephaneLenclud@238: public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown); StephaneLenclud@238: StephaneLenclud@238: StephaneLenclud@238: protected override void DoExecute() StephaneLenclud@238: { StephaneLenclud@238: //First check if the process we want to launch already exists StephaneLenclud@238: string procName = Path.GetFileNameWithoutExtension(File.FullPath); StephaneLenclud@238: Process[] existingProcesses = Process.GetProcessesByName(procName); StephaneLenclud@238: if (existingProcesses == null || existingProcesses.Length == 0 || MultipleInstance) StephaneLenclud@238: { StephaneLenclud@238: // Process do not exists just try to launch it StephaneLenclud@238: ProcessStartInfo start = new ProcessStartInfo(); StephaneLenclud@238: // Enter in the command line arguments, everything you would enter after the executable name itself StephaneLenclud@238: //start.Arguments = arguments; StephaneLenclud@238: // Enter the executable to run, including the complete path StephaneLenclud@238: start.FileName = File.FullPath; StephaneLenclud@238: start.WindowStyle = ProcessWindowStyle.Normal; StephaneLenclud@238: start.CreateNoWindow = true; StephaneLenclud@238: start.UseShellExecute = true; StephaneLenclud@238: // Run the external process & wait for it to finish StephaneLenclud@238: Process proc = Process.Start(start); StephaneLenclud@238: StephaneLenclud@238: //SL: We could have used that too StephaneLenclud@238: //Shell32.Shell shell = new Shell32.Shell(); StephaneLenclud@238: //shell.ShellExecute(Properties.Settings.Default.StartFileName); StephaneLenclud@238: } StephaneLenclud@238: else if (SwitchTo) StephaneLenclud@238: { StephaneLenclud@238: //This won't work properly until we have a manifest that enables uiAccess. StephaneLenclud@238: //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system. StephaneLenclud@238: SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true); StephaneLenclud@238: } StephaneLenclud@238: StephaneLenclud@238: } StephaneLenclud@238: } StephaneLenclud@238: }