SharpLibEar/ActionLaunchApp.cs
author StephaneLenclud
Sat, 20 Aug 2016 21:00:35 +0200
changeset 246 30a221eecc06
parent 238 c92587ddabcd
child 258 e237c2e33545
permissions -rw-r--r--
Generic HID event with key recognition functional.
     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         public override bool IsValid()
    53         {
    54             // This is a valid configuration only if our file exists
    55             return System.IO.File.Exists(File.FullPath);
    56         }
    57 
    58         [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")]
    59         public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown);
    60 
    61 
    62         protected override void DoExecute()
    63         {
    64             //First check if the process we want to launch already exists
    65             string procName = Path.GetFileNameWithoutExtension(File.FullPath);
    66             Process[] existingProcesses = Process.GetProcessesByName(procName);
    67             if (existingProcesses == null || existingProcesses.Length == 0 || MultipleInstance)
    68             {
    69                 // Process do not exists just try to launch it
    70                 ProcessStartInfo start = new ProcessStartInfo();
    71                 // Enter in the command line arguments, everything you would enter after the executable name itself
    72                 //start.Arguments = arguments; 
    73                 // Enter the executable to run, including the complete path
    74                 start.FileName = File.FullPath;
    75                 start.WindowStyle = ProcessWindowStyle.Normal;
    76                 start.CreateNoWindow = true;
    77                 start.UseShellExecute = true;
    78                 // Run the external process & wait for it to finish
    79                 Process proc = Process.Start(start);
    80 
    81                 //SL: We could have used that too
    82                 //Shell32.Shell shell = new Shell32.Shell();
    83                 //shell.ShellExecute(Properties.Settings.Default.StartFileName);
    84             }
    85             else if (SwitchTo)
    86             {
    87                 //This won't work properly until we have a manifest that enables uiAccess.
    88                 //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
    89                 SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
    90             }
    91 
    92         }
    93     }
    94 }