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

时间:2014-01-02 21:58:16

标签: c# visual-studio visual-studio-2012 com-interop

关于如何为VS2010创建项目,我跟着this Stack Overflow post,希望它能指向正确的方向,但不包括创建VS2012项目或解决方案。

我还探讨了使用SLNTools,但我没有看到如何从头开始创建新的解决方案。

最终,我想以编程方式创建3-4个VS2012项目,然后将它们添加到也以编程方式创建的解决方案中。


我尝试了基于this Stack Overflow post的解决方案,但是我得到了一个奇怪的错误。这是代码:

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

这是错误:

enter image description here

4 个答案:

答案 0 :(得分:10)

这对我有用(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机器上,它将使用后者。

答案 1 :(得分:2)

使用.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");

答案 2 :(得分:1)

Jimmy和Xenolightining下面的两个解决方案都有效,但我仍然遇到上述错误的问题。因此,如果其他人遇到该错误,请参阅此链接:

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

总结上面的链接(或万一它被破坏),这就是你要做的。将此类添加到您的解决方案中:

 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);
    }

现在用这些语句包装代码生成代码(来自下面的答案):

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

答案 3 :(得分:1)

我没有Visual Studio 2012 IDE,所以我正在尝试使用Visual Studio 2013

使用C#,我以编程方式创建一个新的Visual Studio 2013解决方案

步骤1:首先,创建窗体表单应用程序,然后在默认窗体1中创建名称为create的按钮。

步骤2:接下来,在创建按钮单击事件中编写以下代码。

class Invoice(models.Model):
    _inherit = 'account.invoice'

    def _method(self):
        # override methods that reference or use the fields we
        # are about to delete so that we don't break core

delattr(odoo.addons.account.models.account_invoice.AccountInvoice, 'account_id')

步骤3:完成这些步骤后,在项目引用中添加envdte.dll引用并构建它 (从系统路径获取envdte.dll&#34; C:\ Program Files(x86)\ Common Files \ microsoft shared \ MSEnv \ PublicAssemblies&#34; /从网上下载)

步骤4:如果您完全填写所有步骤,那么您将以编程方式创建解决方案。