Ejemplo n.º 1
0
        public Project CreateProjectFromTemplate(string template, string projectName, string outputTarget)
        {
            try
            {
                var currentSolutionDirectory = (new FileInfo(CurrentSolution.FullName)).Directory;
                var targetRelativeFolder     = Path.Combine(outputTarget, projectName);
                var targetFolder             = Path.Combine(currentSolutionDirectory.FullName, targetRelativeFolder);
                var targetDirectory          = (new DirectoryInfo(targetFolder));
                var projectFullName          = Path.Combine(targetDirectory.FullName, projectName + ".csproj");

                if (targetDirectory.Exists && File.Exists(projectFullName))
                {
                    //If the project already exists then add it
                    Project retval = null;
                    try
                    {
                        //Get it if exists
                        retval = CurrentSolution.GetProjects().FirstOrDefault(x => x.Name == projectName);

                        //If cannot add then do not show error...might be in solution folder
                        retval = CurrentSolution.AddFromFile(projectFullName, false);
                    }
                    catch (Exception ex)
                    {
                        //Do NOthing
                    }
                    return(retval);
                }

                //If not exists then create it
                if (string.IsNullOrEmpty(outputTarget))
                {
                    return(CurrentSolution.AddFromTemplate(template, targetFolder, projectName));
                }
                else
                {
                    var folder = CreateSolutionFolder(outputTarget);
                    if (!targetDirectory.Exists)
                    {
                        Directory.CreateDirectory(targetFolder);
                    }
                    return((folder.Object as EnvDTE80.SolutionFolder).AddFromTemplate(template, targetFolder, projectName));
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }