Visual Studio の隠しインスタンスをプログラムで作成し、それを使用してソリューションを操作できます。この例では、特定のソリューションに存在するすべてのプロジェクトを一覧表示します。
using System;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
namespace so_sln
{
class Program
{
[STAThread]
static void Main(string[] args)
{
System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0", true);
DTE2 dte = (EnvDTE80.DTE2)System.Activator.CreateInstance(t, true);
// See http://msdn.microsoft.com/en-us/library/ms228772.aspx for the
// code for MessageFilter - just paste it into the so_sln namespace.
MessageFilter.Register();
dte.Solution.Open(@"C:\path\to\my.sln");
foreach (Project project in dte.Solution.Projects)
{
Console.WriteLine(project.Name);
}
dte.Quit();
}
}
public class MessageFilter : IOleMessageFilter
{
... Continues at http://msdn.microsoft.com/en-us/library/ms228772.aspx
(STAThread と MessageFilter のナンセンスは、「外部のマルチスレッド アプリケーションと Visual Studio の間のスレッドの競合の問題が原因である」という意味です。http://msdn.microsoft.com/en-us/library/ からコードを貼り付けます。 ms228772.aspxで動作します。)