Exemplo n.º 1
0
        public static void Execute(UserApp app, ShellBrowserEx shellBrowser)
        {
            // todo validate app
            string pathCurrent;

            using (IDLWrapper wrapper = shellBrowser.GetShellPath()) {
                pathCurrent = wrapper.Path;
            }
            Address[] selection;
            shellBrowser.TryGetSelection(out selection, false);

            List <string> lstFiles = new List <string>();
            List <string> lstDirs  = new List <string>();

            if (selection != null)
            {
                foreach (Address address in selection)
                {
                    using (IDLWrapper wrapper = new IDLWrapper(address.ITEMIDLIST)) {
                        if (!wrapper.Available || !wrapper.HasPath)
                        {
                            continue;
                        }
                        (wrapper.IsFileSystemFile ? lstFiles : lstDirs).Add(address.Path.TrimEnd('\\').Enquote());
                    }
                }
            }

            string strFiles = lstFiles.StringJoin(" ");
            string strDirs  = lstDirs.StringJoin(" ");
            string strBoth  = (strFiles + " " + strDirs).Trim();

            pathCurrent = Directory.Exists(pathCurrent) ? pathCurrent.TrimEnd('\\').Enquote() : "";
            string args = app.Args;
            string work = app.WorkingDir;
            string path = app.Path;

            string[] variableValues =
            {
                strDirs.Length > 0 ? strDirs : pathCurrent,         // %cd%
                pathCurrent,                                        // %c%
                strDirs,                                            // %d%
                strFiles,                                           // %f%
                strBoth                                             // %s%
            };
            for (int i = 0; i < variableValues.Length; i++)
            {
                args = reVariables[i].Replace(args, variableValues[i]);
            }
            variableValues = new string[] {
                lstDirs.Count > 0 ? lstDirs[0] : pathCurrent,       // %cd%
                pathCurrent,                                        // %c%
                lstDirs.Count > 0 ? lstDirs[0] : "",                // %d%
                "",                                                 // %f%
                lstDirs.Count > 0 ? lstDirs[0] : ""                 // %s%
            };
            for (int i = 0; i < variableValues.Length; i++)
            {
                work = reVariables[i].Replace(work, variableValues[i]);
            }

            const int SW_SHOWNORMAL           = 1;
            const int SEE_MASK_IDLIST         = 0x00000004;
            const int SEE_MASK_DOENVSUBST     = 0x00000200; // Expand any environment variables specified in the string given by the lpDirectory or lpFile
            const int SEE_MASK_ASYNCOK        = 0x00100000;
            const int SEE_MASK_FLAG_LOG_USAGE = 0x04000000;

            // Open NameSpace folder.
            if (path.StartsWith(IDLWrapper.INDICATOR_NAMESPACE))
            {
                using (IDLWrapper idlw = new IDLWrapper(path)) {
                    if (idlw.Available)
                    {
                        SHELLEXECUTEINFO sei = new SHELLEXECUTEINFO {
                            cbSize   = Marshal.SizeOf(typeof(SHELLEXECUTEINFO)),
                            nShow    = SW_SHOWNORMAL,
                            fMask    = SEE_MASK_IDLIST,
                            lpIDList = idlw.PIDL,
                            hwnd     = shellBrowser.GetExplorerHandle()
                        };
                        PInvoke.ShellExecuteEx(ref sei);
                        return;
                    }
                }
            }
            else
            {
                // check whether target exists if link
                using (IDLWrapper idlw = new IDLWrapper(path)) {
                    if (idlw.IsLinkToDeadFolder)
                    {
                        return;
                    }
                }

                SHELLEXECUTEINFO sei = new SHELLEXECUTEINFO {
                    cbSize = Marshal.SizeOf(typeof(SHELLEXECUTEINFO)),
                    nShow  = SW_SHOWNORMAL,
                    fMask  = SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_LOG_USAGE | SEE_MASK_ASYNCOK,
                    hwnd   = shellBrowser.GetExplorerHandle()
                };

                try {
                    sei.lpFile = Marshal.StringToCoTaskMemUni(path);

                    // Arguments
                    if (!string.IsNullOrEmpty(args))
                    {
                        sei.lpParameters = Marshal.StringToCoTaskMemUni(args);
                    }

                    // Working directory
                    if (!string.IsNullOrEmpty(work))
                    {
                        work            = work.Trim(new char[] { '"', '\'' });
                        sei.lpDirectory = Marshal.StringToCoTaskMemUni(work);
                    }
                    else
                    {
                        // "::" will cause an exception in Path.GetDirectoryName
                        if (QTUtility2.IsExecutable(Path.GetExtension(path)) &&
                            !path.StartsWith(IDLWrapper.INDICATOR_NAMESPACE))
                        {
                            work = Path.GetDirectoryName(path);
                        }
                        if (!string.IsNullOrEmpty(work))
                        {
                            sei.lpDirectory = Marshal.StringToCoTaskMemUni(work);
                        }
                    }

                    if (PInvoke.ShellExecuteEx(ref sei))
                    {
                        StaticReg.ExecutedPathsList.Add(path);
                        return;
                    }
                }
                finally {
                    if (sei.lpFile != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(sei.lpFile);
                    }
                    if (sei.lpParameters != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(sei.lpParameters);
                    }
                    if (sei.lpDirectory != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(sei.lpDirectory);
                    }
                }
            }

            // Show Error Message.
            if (!String.IsNullOrEmpty(args))
            {
                path += ", " + args;
            }

            if (!String.IsNullOrEmpty(work))
            {
                path += ", " + work;
            }

            MessageBox.Show(
                String.Format(QTUtility.TextResourcesDic["ErrorDialogs"][0], path),
                QTUtility.TextResourcesDic["ErrorDialogs"][1],
                MessageBoxButtons.OK,
                MessageBoxIcon.Error
                );
        }