且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#,我怎么编程方式创建一个新的Visual Studio 2012的解决方案?

更新时间:2023-01-09 16:58:43

这对我的作品(VS2012旗舰版):

This works for me (VS2012 Ultimate):

static void Main(string[] args)
{
    System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
    Object obj = System.Activator.CreateInstance(type, true);
    EnvDTE.DTE dte = (EnvDTE.DTE)obj;
    dte.MainWindow.Visible = true; // optional if you want to See VS doing its thing

    // create a new solution
    dte.Solution.Create(@"C:\NewSolution\", "NewSolution");
    var solution = dte.Solution;

    // create a C# WinForms app
    solution.AddFromTemplate(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows\1033\WindowsApplication\csWindowsApplication.vstemplate",
        @"C:\NewSolution\WinFormsApp", "WinFormsApp");

    // create a C# class library
    solution.AddFromTemplate(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows\1033\ClassLibrary\csClassLibrary.vstemplate",
        @"C:\NewSolution\ClassLibrary", "ClassLibrary");

    // save and quit
    dte.ExecuteCommand("File.SaveAll");
    dte.Quit();
}


HKCR下看,它看起来像有一个VisualStudio.DTE(没有.11.0),将指向VS的最新版本所以,我的机器VS2012和VS2013,它将使用后者。

Looking under HKCR, it looks like there's a VisualStudio.DTE (without the .11.0) that will point to the latest version of VS. So on my machine with VS2012 and VS2013, it will use the latter.