简体   繁体   English

使用C#,如何以编程方式创建新的Visual Studio 2012解决方案?

[英]Using C#, how do I create a new Visual Studio 2012 Solution programmatically?

I followed this Stack Overflow post regarding how to create a project for VS2010, hoping that it would point me in the correct direction, but it doesn't cover creating a VS2012 project or solution. 我关注如何为VS2010创建项目的 Stack Overflow帖子 ,希望它能指向正确的方向,但它不包括创建VS2012项目或解决方案。

I also explored using SLNTools , but I don't see how to create a new solution from scratch. 我还探讨了使用SLNTools ,但我没有看到如何从头开始创建新的解决方案。

Ultimately, I would like to create 3-4 VS2012 projects programmatically and then add them to a solution which is also created programmatically. 最后,我想以编程方式创建3-4个VS2012项目,然后将它们添加到也以编程方式创建的解决方案中。


I attempted a solution based on this Stack Overflow post , but I get an odd error. 我尝试了一个基于Stack Overflow帖子的解决方案,但是我得到了一个奇怪的错误。 Here is the code: 这是代码:

    Type typeDTE = Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
    var dte = (DTE)Activator.CreateInstance(typeDTE, true);
    var sln = (Solution2)dte.Solution;
    sln.Create(@"D:\Visual Studio\Projects","Test");

And here is the error: 这是错误:

在此输入图像描述

This works for me (VS2012 Ultimate): 这对我有用(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();
}

[Edit:] Looking under HKCR, it looks like there's a VisualStudio.DTE (without the .11.0) that will point to the latest version of VS. [编辑:]看看HKCR,看起来有一个VisualStudio.DTE(没有.11.0)将指向最新版本的VS. So on my machine with VS2012 and VS2013, it will use the latter. 因此,在我的VS2012和VS2013机器上,它将使用后者。

Tested and working using .NET4 Winforms, and a reference to EnvDTE100.dll (which should also add references to EnvDTE90.dll, EnvDTE80.dll, and EnvDTE.dll) 使用.NET4 Winforms进行测试和工作,以及对EnvDTE100.dll的引用(还应该添加对EnvDTE90.dll,EnvDTE80.dll和EnvDTE.dll的引用)

Type type = Type.GetTypeFromProgID("VisualStudio.DTE.11.0");
Object obj = System.Activator.CreateInstance(type, true);
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)obj;
EnvDTE100.Solution4 _solution = (EnvDTE100.Solution4)dte.Solution;
_solution.Create(@"C:\Test\", "Test");
_solution.SaveAs(@"C:\Test\Test.sln");

Both solutions below by Jimmy and Xenolightining work, however, I still had the problem of the aforementioned error. Jimmy和Xenolightining下面的两个解决方案都有效,但我仍然遇到了上述错误的问题。 So, in case anyone else encounters that error, see this link: 因此,如果其他人遇到该错误,请参阅此链接:

http://msdn.microsoft.com/en-us/library/ms228772(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ms228772(v=vs.80).aspx

To summarize the link above (or in case it is ever broken), here is what you do. 总结一下上面的链接(或者如果它被打破),这就是你要做的。 Add this class to your solution: 将此类添加到您的解决方案:

 public class MessageFilter : IOleMessageFilter
    {
        //
        // Class containing the IOleMessageFilter
        // thread error-handling functions.

        // Start the filter.
        public static void Register()
        {
            IOleMessageFilter newFilter = new MessageFilter(); 
            IOleMessageFilter oldFilter = null; 
            CoRegisterMessageFilter(newFilter, out oldFilter);
        }

        // Done with the filter, close it.
        public static void Revoke()
        {
            IOleMessageFilter oldFilter = null; 
            CoRegisterMessageFilter(null, out oldFilter);
        }

        //
        // IOleMessageFilter functions.
        // Handle incoming thread requests.
        int IOleMessageFilter.HandleInComingCall(int dwCallType, 
          System.IntPtr hTaskCaller, int dwTickCount, System.IntPtr 
          lpInterfaceInfo) 
        {
            //Return the flag SERVERCALL_ISHANDLED.
            return 0;
        }

        // Thread call was rejected, so try again.
        int IOleMessageFilter.RetryRejectedCall(System.IntPtr 
          hTaskCallee, int dwTickCount, int dwRejectType)
        {
            if (dwRejectType == 2)
            // flag = SERVERCALL_RETRYLATER.
            {
                // Retry the thread call immediately if return >=0 & 
                // <100.
                return 99;
            }
            // Too busy; cancel call.
            return -1;
        }

        int IOleMessageFilter.MessagePending(System.IntPtr hTaskCallee, 
          int dwTickCount, int dwPendingType)
        {
            //Return the flag PENDINGMSG_WAITDEFPROCESS.
            return 2; 
        }

        // Implement the IOleMessageFilter interface.
        [DllImport("Ole32.dll")]
        private static extern int 
          CoRegisterMessageFilter(IOleMessageFilter newFilter, out 
          IOleMessageFilter oldFilter);
    }

    [ComImport(), Guid("00000016-0000-0000-C000-000000000046"), 
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    interface IOleMessageFilter 
    {
        [PreserveSig]
        int HandleInComingCall( 
            int dwCallType, 
            IntPtr hTaskCaller, 
            int dwTickCount, 
            IntPtr lpInterfaceInfo);

        [PreserveSig]
        int RetryRejectedCall( 
            IntPtr hTaskCallee, 
            int dwTickCount,
            int dwRejectType);

        [PreserveSig]
        int MessagePending( 
            IntPtr hTaskCallee, 
            int dwTickCount,
            int dwPendingType);
    }

Now wrap the code-generation code (from answers below) with these statements: 现在用这些语句包装代码生成代码(来自下面的答案):

MessageFilter.Register();
//INSERT YOUR CODE HERE
MessageFilter.Revoke();

I don't have Visual Studio 2012 IDE so I'am trying with Visual Studio 2013 我没有Visual Studio 2012 IDE所以我尝试使用Visual Studio 2013

Using C#,I create a new Visual Studio 2013 Solution programmatically 使用C#,我以编程方式创建一个新的Visual Studio 2013解决方案

Step1: First of all, create window form application and after that to create button with name create in default form1. 步骤1:首先,创建窗体表单应用程序,然后创建默认form1中名称为create的按钮。

Step2: Next,write the following code in create button click event. 步骤2:接下来,在创建按钮单击事件中编写以下代码。

private void btn_Create_Click(object sender, EventArgs e)
      {
          try
          {
              //// Get an instance of the currently running Visual Studio IDE.
              System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE.12.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 12.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 12.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows\1033\ClassLibrary\csClassLibrary.vstemplate",
                  @"C:\NewSolution\ClassLibrary", "ClassLibrary");

              // save and quit
              dte.ExecuteCommand("File.SaveAll");
              dte.Quit();
          }
          catch (Exception ex)
          {
              throw;
          }
      }  

Step3: after these steps,To add envdte.dll reference in project references and build it. Step3:完成这些步骤后,在项目引用中添加envdte.dll引用并构建它。
(To get envdte.dll from your system path "C:\\Program Files (x86)\\Common Files\\microsoft shared\\MSEnv\\PublicAssemblies" / download from net) (从您的系统路径获取envdte.dll“C:\\ Program Files(x86)\\ Common Files \\ microsoft shared \\ MSEnv \\ PublicAssemblies”/从网上下载)

Step4: if you full fill all upon steps then you will create programatically create solution successfully. 第4步:如果您完全填写所有步骤,那么您将以编程方式创建解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式获取有关其他解决方案的信息或进行操作? (Visual Studio,C#) - How can I programmatically get information about or manipulate another solution? (Visual Studio, C#) 如何使用 c# 在 Visual Studio 2010 中以编程方式将现有项目添加到当前解决方案 - How to add an existing project to a current solution programmatically in visual studio 2010 using c# 如何以编程方式在Visual Studio解决方案/程序集中加载文件? - How do I load a file in a Visual Studio solution/assembly programmatically? 在c#下使用Visual Studio 2012创建.sql脚本,以及如何运行它? - create .sql script with Visual Studio 2012 at c# and how run it? Visual Studio 2012 Express C#为什么没有“使用组织”菜单选项? - Visual Studio 2012 Express C# Why do I have no “Organize Using” menu option? 如何在 Visual Studio 的 C# 中创建服务引用? - How do I create a Service Reference in C# in Visual Studio? 如何在 Visual Studio C# 中创建表单实例? - How do I create an instance of a form in Visual Studio C#? 如何在Visual Studio C中创建“项目概述”# - How do I create a “Project overview” in Visual Studio C# Visual Studio 2012 C#解决方案,并行构建竞争条件 - Visual Studio 2012 C# solution, parallel build race condition 我如何评论Visual Studio 2012 C#中的标记文本? (不是线) - How do i comment marked text in Visual Studio 2012 C#? (Not the line)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM