VsTeXProject.VisualStudio.Project.ProjectNode.RunWizard C# (CSharp) Method

RunWizard() private method

private RunWizard ( HierarchyNode parentNode, string itemName, string wizardToRun, IntPtr dlgOwner ) : VSADDRESULT
parentNode HierarchyNode
itemName string
wizardToRun string
dlgOwner IntPtr
return VSADDRESULT
        public virtual VSADDRESULT RunWizard(HierarchyNode parentNode, string itemName, string wizardToRun,
            IntPtr dlgOwner)
        {
            Debug.Assert(!string.IsNullOrEmpty(itemName),
                "The Add item dialog was passing in a null or empty item to be added to the hierrachy.");
            Debug.Assert(!string.IsNullOrEmpty(ProjectFolder), "The Project Folder is not specified for this project.");

            if (parentNode == null)
            {
                throw new ArgumentNullException("parentNode");
            }

            if (string.IsNullOrEmpty(itemName))
            {
                throw new ArgumentNullException("itemName");
            }

            // We just validate for length, since we assume other validation has been performed by the dlgOwner.
            if (ProjectFolder.Length + itemName.Length + 1 > NativeMethods.MAX_PATH)
            {
                var errorMessage = string.Format(CultureInfo.CurrentCulture,
                    SR.GetString(SR.PathTooLong, CultureInfo.CurrentUICulture), itemName);
                if (!Utilities.IsInAutomationFunction(Site))
                {
                    string title = null;
                    var icon = OLEMSGICON.OLEMSGICON_CRITICAL;
                    var buttons = OLEMSGBUTTON.OLEMSGBUTTON_OK;
                    var defaultButton = OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST;
                    VsShellUtilities.ShowMessageBox(Site, title, errorMessage, icon, buttons, defaultButton);
                    return VSADDRESULT.ADDRESULT_Failure;
                }
                throw new InvalidOperationException(errorMessage);
            }


            // Build up the ContextParams safearray
            //  [0] = Wizard type guid  (bstr)
            //  [1] = Project name  (bstr)
            //  [2] = ProjectItems collection (bstr)
            //  [3] = Local Directory (bstr)
            //  [4] = Filename the user typed (bstr)
            //  [5] = Product install Directory (bstr)
            //  [6] = Run silent (bool)

            var contextParams = new object[7];
            contextParams[0] = Constants.vsWizardAddItem;
            contextParams[1] = Caption;
            var automationObject = parentNode.GetAutomationObject();
            if (automationObject is EnvDTE.Project)
            {
                var project = (EnvDTE.Project) automationObject;
                contextParams[2] = project.ProjectItems;
            }
            else
            {
                // This would normally be a folder unless it is an item with subitems
                var item = (ProjectItem) automationObject;
                contextParams[2] = item.ProjectItems;
            }

            contextParams[3] = ProjectFolder;

            contextParams[4] = itemName;

            object objInstallationDir = null;
            var shell = (IVsShell) GetService(typeof (IVsShell));
            ErrorHandler.ThrowOnFailure(shell.GetProperty((int) __VSSPROPID.VSSPROPID_InstallDirectory,
                out objInstallationDir));
            var installDir = (string) objInstallationDir;

            // append a '\' to the install dir to mimic what the shell does (though it doesn't add one to destination dir)
            if (!installDir.EndsWith("\\", StringComparison.Ordinal))
            {
                installDir += "\\";
            }

            contextParams[5] = installDir;

            contextParams[6] = true;

            var ivsExtensibility = GetService(typeof (IVsExtensibility)) as IVsExtensibility3;
            Debug.Assert(ivsExtensibility != null, "Failed to get IVsExtensibility3 service");
            if (ivsExtensibility == null)
            {
                return VSADDRESULT.ADDRESULT_Failure;
            }

            // Determine if we have the trust to run this wizard.
            var wizardTrust = GetService(typeof (SVsDetermineWizardTrust)) as IVsDetermineWizardTrust;
            if (wizardTrust != null)
            {
                var guidProjectAdding = Guid.Empty;
                ErrorHandler.ThrowOnFailure(wizardTrust.OnWizardInitiated(wizardToRun, ref guidProjectAdding));
            }

            int wizResultAsInt;
            try
            {
                Array contextParamsAsArray = contextParams;

                var result = ivsExtensibility.RunWizardFile(wizardToRun, (int) dlgOwner, ref contextParamsAsArray,
                    out wizResultAsInt);

                if (!ErrorHandler.Succeeded(result) && result != VSConstants.OLE_E_PROMPTSAVECANCELLED)
                {
                    ErrorHandler.ThrowOnFailure(result);
                }
            }
            finally
            {
                if (wizardTrust != null)
                {
                    ErrorHandler.ThrowOnFailure(wizardTrust.OnWizardCompleted());
                }
            }

            var wizardResult = (wizardResult) wizResultAsInt;

            switch (wizardResult)
            {
                default:
                    return VSADDRESULT.ADDRESULT_Cancel;
                case wizardResult.wizardResultSuccess:
                    return VSADDRESULT.ADDRESULT_Success;
                case wizardResult.wizardResultFailure:
                    return VSADDRESULT.ADDRESULT_Failure;
            }
        }
ProjectNode