【问题标题】:Adding existing project into new VS2012 solution programmatically fails以编程方式将现有项目添加到新的 VS2012 解决方案中失败
【发布时间】:2012-07-25 10:36:32
【问题描述】:

我们在向导中有以下代码可以将现有项目添加到新解决方案中:

//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(字符串文件名,布尔独占) 在 Wizard.Generator.NewProjectGenerator.Generate(Action`1 logMessage) 在 Wizard.Forms.WizardForm.Finish()

出现此错误后,我手动将新项目添加到解决方案中,一切正常。但我们不能只说“对不起,我们无法为您添加新生成的项目,请您自行添加。”

MSDN 建议:

如果您想在执行期间禁止其 UI,您可以使用 LaunchWizard 方法而不是 AddFromFile 来执行向导。 LaunchWizard 有一个参数可以让您禁用 UI。

但是这种方法需要一些向导文件,所以不能解决。

有人可以帮忙吗? 向导正在从“新建 -> 项目”菜单运行。

【问题讨论】:

  • 你想使用什么 COM 文件?此外,您是否已将此情况报告给 Microsoft?它完全有可能只是一个错误。
  • 我正在使用下一个 dll:EnvDTE [8.0.0.0]、EnvDTE80 [8.0.0.0]、Extensibility [7.0.3300.0]、VSLangProj [7.0.3300.0]、Microsoft Visual Studio Command Bars 8.0 , Microsoft.VisualStudio.OLE.Interop [7.1.40304.0], Microsoft.VisualStudio.Shell.Interop [7.1.40304.0], Microsoft.VisualStudio.Shell.Interop.8.0
  • 谢谢,我会尝试联系 Microsoft 支持。
  • 我已经创建了错误报告:connect.microsoft.com/VisualStudio/feedback/details/755456/… 如果您遇到同样的问题,请投票给它。

标签: c# visual-studio-2012 envdte


【解决方案1】:

这里是问题的解决方法(由我的老板提出):

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

也许有人知道如何做到这一点很棒?我的意思是转换。我会让这个问题在一段时间内无人回答。等待你的cmets。

【讨论】:

  • 运行“devenv.exe /upgrade PROJECT_FILE”
猜你喜欢
  • 2010-09-19
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多