SharpLibEar/ActionLaunchApp.cs
author StephaneLenclud
Thu, 18 Aug 2016 14:35:50 +0200
changeset 238 c92587ddabcd
child 239 dd7770b97916
permissions -rw-r--r--
Support for launch action and WMC HID events.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.IO;
     5 using System.Linq;
     6 using System.Runtime.Serialization;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 
    10 namespace SharpLib.Ear
    11 {
    12     [DataContract]
    13     [AttributeObject(Id = "Action.Launch.App", Name = "Launch application", Description = "Launch an application.")]
    14     public class ActionLaunchApp : Action
    15     {
    16         [DataMember]
    17         [AttributeObjectProperty
    18             (
    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"
    23             )
    24         ]
    25         public PropertyFile File { get; set; } = new PropertyFile();
    26 
    27         [DataMember]
    28         [AttributeObjectProperty
    29             (
    30             Id = "Action.Launch.App.SwitchTo",
    31             Name = "Switch to",
    32             Description = "Specifies if we should switch the application if it's already launched."
    33             )
    34         ]
    35         public bool SwitchTo { get; set; } = true;
    36 
    37         [DataMember]
    38         [AttributeObjectProperty
    39             (
    40             Id = "Action.Launch.App.MultipleInstance",
    41             Name = "Multiple Instance",
    42             Description = "Specifies if We should launch multiple instance."
    43             )
    44         ]
    45         public bool MultipleInstance { get; set; } = false;
    46 
    47         public override string Brief()
    48         {
    49             return Name + ": " + Path.GetFileName(File.FullPath);
    50         }
    51 
    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);
    54 
    55 
    56         protected override void DoExecute()
    57         {
    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)
    62             {
    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);
    74 
    75                 //SL: We could have used that too
    76                 //Shell32.Shell shell = new Shell32.Shell();
    77                 //shell.ShellExecute(Properties.Settings.Default.StartFileName);
    78             }
    79             else if (SwitchTo)
    80             {
    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);
    84             }
    85 
    86         }
    87     }
    88 }