可以调用EnvDTE,操作Visual Studio,向已经打开的解决方案中添加工程,或者工程中添加文件等等操作
需要引入envdte开头的几个动态库,VSLangProj开头的几个动态库,下面是我写的一个简单的添加工程的例子
using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace EnvDTESolution
{
public class CNxEnvDTEHelper
{
[DllImport("ole32.dll")]
private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc);
[DllImport("ole32.dll")]
private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot);
public static Project AddProjectToDTE(_DTE dte, string projectName, bool Exclusive = false)
{
if (dte == null || dte.Solution == null)
{
return null;
}
try
{
return dte.Solution.AddFromFile(projectName, Exclusive);
}
catch (System.Exception ex)
{
//log
return null;
}
}
public static IList<_DTE> GetAllInstances()
{
IList<_DTE> dtes = new List<_DTE>();
try
{
IRunningObjectTable runningObjectTable;
IEnumMoniker enumMoniker;
int retVal = GetRunningObjectTable(0, out runningObjectTable);
if (retVal == 0)
{
runningObjectTable.EnumRunning(out enumMoniker);
IntPtr fetched = IntPtr.Zero;
IMoniker[] moniker = new IMoniker[1];
object punkObject = null;
while (enumMoniker.Next(1, moniker, fetched) == 0)
{
IBindCtx bindCtx;
CreateBindCtx(0, out bindCtx);
string displayName;
moniker[0].GetDisplayName(bindCtx, null, out displayName);
bool isVisualStudio = displayName.StartsWith("!VisualStudio");
if (isVisualStudio)
{
runningObjectTable.GetObject(moniker[0], out punkObject);
_DTE dte = punkObject as _DTE;
if (dte != null)
{
dtes.Add(dte);
}
}
}
}
}
catch (System.Exception ex)
{
//log
}
return dtes;
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。