Support for launch action and WMC HID events.
2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using System.Runtime.Serialization;
8 using System.Threading.Tasks;
10 namespace SharpLib.Ear
13 [AttributeObject(Id = "Action.Launch.App", Name = "Launch application", Description = "Launch an application.")]
14 public class ActionLaunchApp : Action
17 [AttributeObjectProperty
19 Id = "Action.Launch.App.File",
20 Name = "File to launch",
21 Description = "Specifies the application file to launch.",
22 Filter = "EXE files (*.exe)|*.exe"
25 public PropertyFile File { get; set; } = new PropertyFile();
28 [AttributeObjectProperty
30 Id = "Action.Launch.App.SwitchTo",
32 Description = "Specifies if we should switch the application if it's already launched."
35 public bool SwitchTo { get; set; } = true;
38 [AttributeObjectProperty
40 Id = "Action.Launch.App.MultipleInstance",
41 Name = "Multiple Instance",
42 Description = "Specifies if We should launch multiple instance."
45 public bool MultipleInstance { get; set; } = false;
47 public override string Brief()
49 return Name + ": " + Path.GetFileName(File.FullPath);
52 [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")]
53 public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown);
56 protected override void DoExecute()
58 //First check if the process we want to launch already exists
59 string procName = Path.GetFileNameWithoutExtension(File.FullPath);
60 Process[] existingProcesses = Process.GetProcessesByName(procName);
61 if (existingProcesses == null || existingProcesses.Length == 0 || MultipleInstance)
63 // Process do not exists just try to launch it
64 ProcessStartInfo start = new ProcessStartInfo();
65 // Enter in the command line arguments, everything you would enter after the executable name itself
66 //start.Arguments = arguments;
67 // Enter the executable to run, including the complete path
68 start.FileName = File.FullPath;
69 start.WindowStyle = ProcessWindowStyle.Normal;
70 start.CreateNoWindow = true;
71 start.UseShellExecute = true;
72 // Run the external process & wait for it to finish
73 Process proc = Process.Start(start);
75 //SL: We could have used that too
76 //Shell32.Shell shell = new Shell32.Shell();
77 //shell.ShellExecute(Properties.Settings.Default.StartFileName);
81 //This won't work properly until we have a manifest that enables uiAccess.
82 //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
83 SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);