Adding existing project into new VS2012 solution programmatically fails

2

We have the following code in wizard to add existing project to a new solution:

//generating files
if (dte.Solution.Projects.Count < 1) // Solution is empty or doesn't exist
{
    dte.Solution.Create(oneFolderHigher(Params.OutputDir, solutionName),
                        solutionFileName(solutionName));
}

// adding created project to solution
dte.Solution.AddFromFile(Path.Combine(Params.ProjectRootFolder, 
                                      Params.ProjectName + ".csproj"));

It works just fine under MS Visual Studio 2010, but fails under 2012 (I experimented with second parameter):

System.Runtime.InteropServices.COMException (0x80004004): Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)) at EnvDTE.SolutionClass.AddFromFile(String FileName, Boolean Exclusive) at Wizard.Generator.NewProjectGenerator.Generate(Action`1 logMessage) at Wizard.Forms.WizardForm.Finish()

After this error I'm adding the new project to the solution manually and everything works OK. But we can not just say, "Sorry, we can not add newly generated project for you so please add it by yourself."

MSDN proposes:

You can use the LaunchWizard method rather than AddFromFile to execute a wizard if you want to suppress its UI during execution. LaunchWizard has a parameter that allows you to disable the UI.

But this method requires some wizard file, so it can not be a solution.

Could someone help? Wizard is running from "New -> Project" menu.

c#
visual-studio-2012
envdte
asked on Stack Overflow Jul 25, 2012 by Taras Kozubski • edited Jul 25, 2012 by (unknown user)

1 Answer

0

Here the workaround for the issue (proposed by my boss):

Before adding the project to solution, project file should be converted to
VS2012 format.

But code is little ugly:

using (StreamReader sr = new StreamReader(newFile))
using (StreamWriter sw = new StreamWriter(projectFile, false, Encoding.UTF8))
{
     while (sr.Peek() >= 0)
     {
         string s = sr.ReadLine();
         if (s.Contains("<Project ToolsVersion=\"4.0\""))
         {
              s = s + Environment.NewLine + importProject;
         }
... and so on

Maybe someone knows the way to do it awesome? I mean converting. I'll let the question to be unanswered some time. Waiting for your comments.

answered on Stack Overflow Jul 26, 2012 by Taras Kozubski

User contributions licensed under CC BY-SA 3.0