Microsoft.VisualStudio.Project.RegisteredProjectType.GetVSTemplateFile C# (CSharp) Method

GetVSTemplateFile() public method

If the project support VsTemplates, returns the path to the vstemplate file corresponding to the requested template You can pass in a string such as: "Windows\Console Application"
public GetVSTemplateFile ( string templateFile ) : string
templateFile string
return string
        public virtual string GetVSTemplateFile(string templateFile)
        {
            // First see if this use the vstemplate model
            if(!String.IsNullOrEmpty(DefaultProjectExtensionValue))
            {
                EnvDTE80.DTE2 dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
                if(dte != null)
                {
                    EnvDTE80.Solution2 solution = dte.Solution as EnvDTE80.Solution2;
                    if(solution != null)
                    {
                        string fullPath = solution.GetProjectTemplate(templateFile, DefaultProjectExtensionValue);
                        // The path returned by GetProjectTemplate can be in the format "path|FrameworkVersion=x.y|Language=xxx"
                        // where the framework version and language sections are optional.
                        // Here we are interested only in the full path, so we have to remove all the other sections.
                        int pipePos = fullPath.IndexOf('|');
                        if(0 == pipePos)
                        {
                            return null;
                        }
                        if(pipePos > 0)
                        {
                            fullPath = fullPath.Substring(0, pipePos);
                        }
                        return fullPath;
                    }
                }

            }
            return null;
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Based on the Template and TypeGuid properties of the
        /// element, generate the full template path.
        ///
        /// TypeGuid should be the Guid of a registered project factory.
        /// Template can be a full path, a project template (for projects
        /// that support VsTemplates) or a relative path (for other projects).
        /// </summary>
        protected virtual string GetProjectTemplatePath(ProjectElement element)
        {
            ProjectElement elementToUse = (element == null) ? this.nestedProjectElement : element;

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

            string templateFile = elementToUse.GetMetadata(ProjectFileConstants.Template);

            Debug.Assert(!String.IsNullOrEmpty(templateFile), "No template file has been specified in the template attribute in the project file");

            string fullPath = templateFile;

            if (!Path.IsPathRooted(templateFile))
            {
                RegisteredProjectType registeredProjectType = this.GetRegisteredProject(elementToUse);

                // This is not a full path
                Debug.Assert(registeredProjectType != null && (!String.IsNullOrEmpty(registeredProjectType.DefaultProjectExtensionValue) || !String.IsNullOrEmpty(registeredProjectType.WizardTemplatesDirValue)), " Registered wizard directory value not set in the registry.");

                // See if this specify a VsTemplate file
                fullPath = registeredProjectType.GetVSTemplateFile(templateFile);
                if (String.IsNullOrEmpty(fullPath))
                {
                    // Default to using the WizardTemplateDir to calculate the absolute path
                    fullPath = Path.Combine(registeredProjectType.WizardTemplatesDirValue, templateFile);
                }
            }

            return(fullPath);
        }