2012-07-25 53 views
2

我們在嚮導下面的代碼對現有項目添加到一個新的解決方案:添加現有項目到新的VS2012解決方案以編程失敗

//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")); 

它可以在MS Visual Studio 2010的就好了,但2012下失敗(我嘗試用第二參數):

System.Runtime.InteropServices.COMException(0x80004004):操作中止(從HRESULT異常:0x80004004(E_ABORT)) 在EnvDTE.SolutionClass.AddFromFile(字符串文件名,布爾異) at Wizard.Generator.NewProjectGenerator.Generate(Action`1的LogMessage) 在Wizard.Forms.WizardForm.Finish()

此錯誤後我加入了新的項目,以手動解決方案,一切工作正常。但我們不能只是說,「對不起,我們不能爲您添加新生成的項目,所以請自行添加。」

MSDN提出:

可以使用LaunchWizard方法,而不是AddFromFile執行嚮導,如果你想在執行過程中抑制其UI。 LaunchWizard有一個參數,允許您禁用UI。

但是這種方法需要一些嚮導文件,所以它不能成爲解決方案。

有人能幫忙嗎? 嚮導正在從「新建 - >項目」菜單中運行。

+0

你想要使用什麼COM文件?此外,你有沒有向微軟報告過?它完全有可能只是一個bug。 – 2012-07-25 11:04:59

+0

我正在使用下一個dll:EnvDTE [8.0.0.0],EnvDTE80 [8.0.0.0],擴展性[7.0.3300.0],VSLangProj [7.0.3300.0],Microsoft Visual Studio命令欄8.0,Microsoft.VisualStudio.OLE。 Interop [7.1.40304.0],Microsoft.VisualStudio.Shell.Interop [7.1.40304.0],Microsoft.VisualStudio.Shell.Interop.8.0 – 2012-07-25 12:28:18

+0

謝謝,我將嘗試與Microsoft支持聯繫。 – 2012-07-25 12:29:45

回答

0

這裏的解決方法的問題(我的老闆建議):

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

但代碼是難看一點:

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 

也許有人知道做真棒的方式嗎?我的意思是轉換。我會讓這個問題有一段時間沒有答案。靜候你的評價。

+2

運行「devenv.exe/upgrade PROJECT_FILE」 – tartakynov 2012-10-05 08:46:50

相關問題