示例#1
0
        private static async void HandleApplicationLaunch(string application, AppServiceRequestReceivedEventArgs args)
        {
            var arguments        = args.Request.Message.Get("Arguments", "");
            var workingDirectory = args.Request.Message.Get("WorkingDirectory", "");

            try
            {
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName        = application;
                // Show window if workingDirectory (opening terminal)
                process.StartInfo.CreateNoWindow = string.IsNullOrEmpty(workingDirectory);
                if (arguments == "runas")
                {
                    process.StartInfo.UseShellExecute = true;
                    process.StartInfo.Verb            = "runas";
                    if (Path.GetExtension(application).ToLower() == ".msi")
                    {
                        process.StartInfo.FileName  = "msiexec.exe";
                        process.StartInfo.Arguments = $"/a \"{application}\"";
                    }
                }
                else if (arguments == "runasuser")
                {
                    process.StartInfo.UseShellExecute = true;
                    process.StartInfo.Verb            = "runasuser";
                    if (Path.GetExtension(application).ToLower() == ".msi")
                    {
                        process.StartInfo.FileName  = "msiexec.exe";
                        process.StartInfo.Arguments = $"/i \"{application}\"";
                    }
                }
                else
                {
                    process.StartInfo.Arguments = arguments;
                }
                process.StartInfo.WorkingDirectory = workingDirectory;
                process.Start();
            }
            catch (Win32Exception)
            {
                Process process = new Process();
                process.StartInfo.UseShellExecute  = true;
                process.StartInfo.Verb             = "runas";
                process.StartInfo.FileName         = application;
                process.StartInfo.CreateNoWindow   = true;
                process.StartInfo.Arguments        = arguments;
                process.StartInfo.WorkingDirectory = workingDirectory;
                try
                {
                    process.Start();
                }
                catch (Win32Exception)
                {
                    try
                    {
                        await Win32API.StartSTATask(() =>
                        {
                            var split = application.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => GetMtpPath(x));
                            if (split.Count() == 1)
                            {
                                Process.Start(split.First());
                            }
                            else
                            {
                                var groups = split.GroupBy(x => new
                                {
                                    Dir  = Path.GetDirectoryName(x),
                                    Prog = Win32API.GetFileAssociation(x).Result ?? Path.GetExtension(x)
                                });
                                foreach (var group in groups)
                                {
                                    if (!group.Any())
                                    {
                                        continue;
                                    }
                                    using var cMenu = Win32API.ContextMenu.GetContextMenuForFiles(group.ToArray(), Shell32.CMF.CMF_DEFAULTONLY);
                                    cMenu?.InvokeVerb(Shell32.CMDSTR_OPEN);
                                }
                            }
                            return(true);
                        });
                    }
                    catch (Win32Exception)
                    {
                        // Cannot open file (e.g DLL)
                    }
                    catch (ArgumentException)
                    {
                        // Cannot open file (e.g DLL)
                    }
                }
            }
        }
示例#2
0
        private static async void HandleApplicationLaunch(string application, AppServiceRequestReceivedEventArgs args)
        {
            var arguments        = args.Request.Message.Get("Arguments", "");
            var workingDirectory = args.Request.Message.Get("WorkingDirectory", "");

            try
            {
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName        = application;
                // Show window if workingDirectory (opening terminal)
                process.StartInfo.CreateNoWindow = string.IsNullOrEmpty(workingDirectory);
                if (arguments == "runas")
                {
                    process.StartInfo.UseShellExecute = true;
                    process.StartInfo.Verb            = "runas";
                    if (Path.GetExtension(application).ToLower() == ".msi")
                    {
                        process.StartInfo.FileName  = "msiexec.exe";
                        process.StartInfo.Arguments = $"/a \"{application}\"";
                    }
                }
                else if (arguments == "runasuser")
                {
                    process.StartInfo.UseShellExecute = true;
                    process.StartInfo.Verb            = "runasuser";
                    if (Path.GetExtension(application).ToLower() == ".msi")
                    {
                        process.StartInfo.FileName  = "msiexec.exe";
                        process.StartInfo.Arguments = $"/i \"{application}\"";
                    }
                }
                else
                {
                    process.StartInfo.Arguments = arguments;
                }
                process.StartInfo.WorkingDirectory = workingDirectory;
                process.Start();
            }
            catch (Win32Exception)
            {
                Process process = new Process();
                process.StartInfo.UseShellExecute  = true;
                process.StartInfo.Verb             = "runas";
                process.StartInfo.FileName         = application;
                process.StartInfo.CreateNoWindow   = true;
                process.StartInfo.Arguments        = arguments;
                process.StartInfo.WorkingDirectory = workingDirectory;
                try
                {
                    process.Start();
                }
                catch (Win32Exception)
                {
                    try
                    {
                        await Win32API.StartSTATask(() =>
                        {
                            var split = application.Split(';').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => GetMtpPath(x));
                            if (split.Count() == 1)
                            {
                                Process.Start(split.First());
                            }
                            else
                            {
                                var groups = split.GroupBy(x => new
                                {
                                    Dir  = Path.GetDirectoryName(x),
                                    Prog = Win32API.GetFileAssociation(x).Result ?? Path.GetExtension(x)
                                });
                                foreach (var group in groups)
                                {
                                    if (!group.Any())
                                    {
                                        continue;
                                    }

                                    var files    = group.Select(x => new ShellItem(x));
                                    using var sf = files.First().Parent;
                                    Shell32.IContextMenu menu = null;
                                    try
                                    {
                                        menu = sf.GetChildrenUIObjects <Shell32.IContextMenu>(null, files.ToArray());
                                        menu.QueryContextMenu(HMENU.NULL, 0, 0, 0, Shell32.CMF.CMF_DEFAULTONLY);
                                        var pici    = new Shell32.CMINVOKECOMMANDINFOEX();
                                        pici.lpVerb = Shell32.CMDSTR_OPEN;
                                        pici.nShow  = ShowWindowCommand.SW_SHOW;
                                        pici.cbSize = (uint)Marshal.SizeOf(pici);
                                        menu.InvokeCommand(pici);
                                    }
                                    finally
                                    {
                                        foreach (var elem in files)
                                        {
                                            elem.Dispose();
                                        }

                                        if (menu != null)
                                        {
                                            Marshal.ReleaseComObject(menu);
                                        }
                                    }
                                }
                            }
                            return(true);
                        });
                    }
                    catch (Win32Exception)
                    {
                        // Cannot open file (e.g DLL)
                    }
                    catch (ArgumentException)
                    {
                        // Cannot open file (e.g DLL)
                    }
                }
            }
        }
示例#3
0
        private static void HandleApplicationLaunch(AppServiceRequestReceivedEventArgs args)
        {
            var arguments = args.Request.Message.Get <string, string, object>("Arguments");

            try
            {
                var     executable = (string)args.Request.Message["Application"];
                Process process    = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.FileName        = executable;
                // Show window if arguments (opening terminal)
                process.StartInfo.CreateNoWindow = string.IsNullOrEmpty(arguments);
                process.StartInfo.Arguments      = arguments;
                process.Start();
            }
            catch (Win32Exception)
            {
                var     executable = (string)args.Request.Message["Application"];
                Process process    = new Process();
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.Verb            = "runas";
                process.StartInfo.FileName        = executable;
                process.StartInfo.CreateNoWindow  = true;
                process.StartInfo.Arguments       = arguments;
                try
                {
                    process.Start();
                }
                catch (Win32Exception)
                {
                    try
                    {
                        var split = executable.Split(';').Where(x => !string.IsNullOrWhiteSpace(x));
                        if (split.Count() == 1)
                        {
                            Process.Start(executable);
                        }
                        else
                        {
                            var groups = split.GroupBy(x => new {
                                Dir  = Path.GetDirectoryName(x),
                                Prog = Win32API.GetFileAssociation(x).Result ?? Path.GetExtension(x)
                            });
                            foreach (var group in groups)
                            {
                                if (!group.Any())
                                {
                                    continue;
                                }
                                var files = group.Select(x => new ShellItem(x));
                                using var sf = files.First().Parent;
                                Vanara.PInvoke.Shell32.IContextMenu menu = null;
                                try
                                {
                                    menu = sf.GetChildrenUIObjects <Vanara.PInvoke.Shell32.IContextMenu>(null, files.ToArray());
                                    menu.QueryContextMenu(Vanara.PInvoke.HMENU.NULL, 0, 0, 0, Vanara.PInvoke.Shell32.CMF.CMF_DEFAULTONLY);
                                    var pici = new Vanara.PInvoke.Shell32.CMINVOKECOMMANDINFOEX();
                                    pici.lpVerb = Vanara.PInvoke.Shell32.CMDSTR_OPEN;
                                    pici.nShow  = Vanara.PInvoke.ShowWindowCommand.SW_SHOW;
                                    pici.cbSize = (uint)Marshal.SizeOf(pici);
                                    menu.InvokeCommand(pici);
                                }
                                finally
                                {
                                    foreach (var elem in files)
                                    {
                                        elem.Dispose();
                                    }
                                    if (menu != null)
                                    {
                                        Marshal.ReleaseComObject(menu);
                                    }
                                }
                            }
                        }
                    }
                    catch (Win32Exception)
                    {
                        // Cannot open file (e.g DLL)
                    }
                    catch (ArgumentException)
                    {
                        // Cannot open file (e.g DLL)
                    }
                }
            }
        }