gpt4 book ai didi

c# - 在 EnvDTE 中重新加载卸载的项目

转载 作者:太空狗 更新时间:2023-10-29 20:28:05 28 4
gpt4 key购买 nike

我已经使用 EnvDTE 列出了我的解决方案中的所有项目,但我在我的代码中发现了一个错误:我无法获取已卸载的项目。

我找到了一种跳过卸载项目的方法:

if (string.Compare(EnvDTE.Constants.vsProjectKindUnmodeled, project.Kind, System.StringComparison.OrdinalIgnoreCase) == 0)
continue;

这样,我的代码不会崩溃 - 但我无法通过代码加载丢失的项目,因为它们已经存在。

如何将卸载的项目加载到解决方案中?

我试过:

project.DTE.ExecuteCommand("Project.ReloadProject");

出现错误:

System.Runtime.InteropServices.COMException (...): 命令“Project.ReloadProject”不可用。

所以我试图以某种方式得到

application.DTE.ExecuteCommand("Project.ReloadProject");

但在此之前,从我在 NET 上搜索的每个地方,我必须预先选择解决方案中的项目 - 为此,我需要 project.Name(我有),并且路径,我不知道(我发现的每个示例都假定解决方案路径与项目路径相同,这在一般情况下极不可能)。

最佳答案

Visual Studio SDK 显然是执行此操作的方法。

var dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
Microsoft.VisualStudio.Shell.Interop.IVsUIHierarchyWindow hierarchy;
ServiceProvider sp = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte);
IVsSolution sol = (IVsSolution)sp.GetService(typeof(SVsSolution));

foreach (ProjInfo info in GetProjectInfo(sol))
{
info.Dump();

}
//from http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/60fdd7b4-2247-4c18-b1da-301390edabf3/
static IEnumerable<ProjInfo> GetProjectInfo(IVsSolution sol)
{
Guid ignored = Guid.Empty;
IEnumHierarchies hierEnum;
if (ErrorHandler.Failed(sol.GetProjectEnum((int)__VSENUMPROJFLAGS.EPF_ALLPROJECTS, ref ignored, out hierEnum)))
{
yield break;
}

IVsHierarchy[] hier = new IVsHierarchy[1];
uint fetched;
while ((hierEnum.Next((uint)hier.Length, hier, out fetched) == VSConstants.S_OK) && (fetched == hier.Length))
{
int res = (int)VSConstants.S_OK;

Guid projGuid;
if (ErrorHandler.Failed(res = sol.GetGuidOfProject(hier[0], out projGuid)))
{
Debug.Fail(String.Format("IVsolution::GetGuidOfProject returned 0x{0:X}.", res));
continue;
}

string uniqueName;
if (ErrorHandler.Failed(res = sol.GetUniqueNameOfProject(hier[0], out uniqueName)))
{
Debug.Fail(String.Format("IVsolution::GetUniqueNameOfProject returned 0x{0:X}.", res));
continue;
}
if( System.IO.Path.GetInvalidPathChars().Any (p =>uniqueName.Contains(p) ))
{
uniqueName.Dump("invalid filename found");
yield return new ProjInfo(projGuid,uniqueName);
}
else {
yield return new ProjInfo(projGuid, Path.GetFileName(uniqueName).BeforeOrSelf("{"));
}
}
}

大部分来自 http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/60fdd7b4-2247-4c18-b1da-301390edabf3/

关于c# - 在 EnvDTE 中重新加载卸载的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13770409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com