使用C#,我怎么编程方式创建一个新的Visual Studio 2012的解决方案? [英] Using C#, how do I create a new Visual Studio 2012 Solution programmatically?
问题描述
我跟着这对于如何创建一个 >项目作为VS2010,希望它会点我在正确的方向,但它不包括创建一个VS2012项目或解决方案。
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.
我也探索使用 SLNTools ,但我看不出如何从头开始创建一个新的解决方案。
I also explored using SLNTools, but I don't see how to create a new solution from scratch.
最后,我想以编程方式创建3-4 VS2012的项目,然后将它们添加到这也是编程方式创建一个解决方案。
Ultimately, I would like to create 3-4 VS2012 projects programmatically and then add them to a solution which is also created programmatically.
任何帮助表示赞赏。
我试图根据的this堆栈溢出帖子,但我得到一个奇怪的错误。下面是代码:
I attempted a solution based on this Stack Overflow post, but I get an odd error. 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");
和这里是错误:
下面这两种解决方案由吉米和Xenolightining工作,但是,我仍然有前面提到的错误的问题。因此,如果任何人遇到错误,请参阅此链接:
Both solutions below by Jimmy and Xenolightining work, however, I still had the problem of the aforementioned error. So, in case anyone else encounters that error, see this link:
的 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();
干杯!
Cheers!
推荐答案
这对我的作品(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.
这篇关于使用C#,我怎么编程方式创建一个新的Visual Studio 2012的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!