我一直关注MSDN's Hello World guide to developing Visual Studio extensions(本文专门介绍如何创建一个作为Visual Studio工具栏命令)。
我正在尝试列出当前/活动解决方案中包含的所有项目。
在Command模板的自动生成代码中。
我已尝试EnvDTE
的{{1}} Solution
属性,但它显示零项目。
还有一个Projects
属性,但它也显示一个空数组。
这是如何实现的?
P.S。:我尝试了DTE和DTE2接口,因为从文档中理解使用哪个版本令人困惑。我得到DTE2的空服务,所以我要去DTE。
My Solution Explorer如下所示:
更新:来自gitter/extendvs的Bert Huijben建议在VSSDK Extensibility Samples找到以下内容,但这也不起作用(返回0个元素,两者都在构造函数中并在回调函数中):
ActiveSolutionProjects
答案 0 :(得分:3)
获取EnvDTE.Project对象:
static private void FindProjectsIn(EnvDTE.ProjectItem item, List<EnvDTE.Project> results)
{
if (item.Object is EnvDTE.Project)
{
var proj = (EnvDTE.Project)item.Object;
if (new Guid(proj.Kind) != Utilities.ProjectTypeGuids.Folder)
{
results.Add((EnvDTE.Project)item.Object);
}
else
{
foreach (EnvDTE.ProjectItem innerItem in proj.ProjectItems)
{
FindProjectsIn(innerItem, results);
}
}
}
if (item.ProjectItems != null)
{
foreach (EnvDTE.ProjectItem innerItem in item.ProjectItems)
{
FindProjectsIn(innerItem, results);
}
}
}
static private void FindProjectsIn(EnvDTE.UIHierarchyItem item, List<EnvDTE.Project> results)
{
if (item.Object is EnvDTE.Project)
{
var proj = (EnvDTE.Project)item.Object;
if (new Guid(proj.Kind) != Utilities.ProjectTypeGuids.Folder)
{
results.Add((EnvDTE.Project)item.Object);
}
else
{
foreach (EnvDTE.ProjectItem innerItem in proj.ProjectItems)
{
FindProjectsIn(innerItem, results);
}
}
}
foreach (EnvDTE.UIHierarchyItem innerItem in item.UIHierarchyItems)
{
FindProjectsIn(innerItem, results);
}
}
static internal IEnumerable<EnvDTE.Project> GetEnvDTEProjectsInSolution()
{
List<EnvDTE.Project> ret = new List<EnvDTE.Project>();
EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE));
EnvDTE.UIHierarchy hierarchy = dte.ToolWindows.SolutionExplorer;
foreach (EnvDTE.UIHierarchyItem innerItem in hierarchy.UIHierarchyItems)
{
FindProjectsIn(innerItem, ret);
}
return ret;
}
值得注意的是,需要递归才能深入研究解决方案文件夹。
如果您只想要文件路径,可以使用DTE执行此操作:
static internal string[] GetProjectFilesInSolution()
{
IVsSolution sol = ServiceProvider.GlobalProvider.GetService(typeof(SVsSolution)) as IVsSolution;
uint numProjects;
ErrorHandler.ThrowOnFailure(sol.GetProjectFilesInSolution((uint)__VSGETPROJFILESFLAGS.GPFF_SKIPUNLOADEDPROJECTS, 0, null, out numProjects));
string[] projects = new string[numProjects];
ErrorHandler.ThrowOnFailure(sol.GetProjectFilesInSolution((uint)__VSGETPROJFILESFLAGS.GPFF_SKIPUNLOADEDPROJECTS, numProjects, projects, out numProjects));
//GetProjectFilesInSolution also returns solution folders, so we want to do some filtering
//things that don't exist on disk certainly can't be project files
return projects.Where(p => !string.IsNullOrEmpty(p) && System.IO.File.Exists(p)).ToArray();
}
答案 1 :(得分:1)
适合我:
在包中添加一个字段用于dte。 获得DTE服务。 参考解决方案。
using EnvDTE;
using EnvDTE80;
在你的构造函数中:
dte = this.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
在命令处理程序中:
Integer count = ((EnvDTE.SolutionClass)dte.Solution).Projects.Count;
我从中获得了正确的计数。
屏幕截图(请求)
代码
//------------------------------------------------------------------------------
// <copyright file="Command1.cs" company="Company">
// Copyright (c) Company. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE;
using EnvDTE80;
namespace SolExpExt
{
internal sealed class Command1
{
public const int CommandId = 0x0100;
public static readonly Guid CommandSet = new Guid("beff5a1a-dff5-4f6a-95c8-fd7ea7411a7b");
private DTE2 dte;
private readonly Package package;
private IVsSolution sol;
private Command1(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
commandService.AddCommand(menuItem);
}
dte = this.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
}
public static Command1 Instance
{
get;
private set;
}
private IServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
public static void Initialize(Package package)
{
Instance = new Command1(package);
}
private void MenuItemCallback(object sender, EventArgs e)
{
string message = $"There are {dte.Solution.Projects.Count} projects in this solution.";
string title = "Command1";
VsShellUtilities.ShowMessageBox(
this.ServiceProvider,
message,
title,
OLEMSGICON.OLEMSGICON_INFO,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
}
}
答案 2 :(得分:1)
很不幸,在这里找不到任何可行的解决方案,因此决定发布我自己的解决方案:
/// <summary>
/// Queries for all projects in solution, recursively (without recursion)
/// </summary>
/// <param name="sln">Solution</param>
/// <returns>List of projects</returns>
static List<Project> GetProjects(Solution sln)
{
List<Project> list = new List<Project>();
list.AddRange(sln.Projects.Cast<Project>());
for (int i = 0; i < list.Count; i++)
// OfType will ignore null's.
list.AddRange(list[i].ProjectItems.Cast<ProjectItem>().Select(x => x.SubProject).OfType<Project>());
return list;
}
如果您不知道要添加哪些引用/名称空间,则可以从此处获取带有源代码的项目:
并检查名称空间/引用。
答案 3 :(得分:0)
以下,从AutoFindReplace无耻地采取,使用VS2015社区:
using EnvDTE;
.
.
protected override void Initialize()
{
base.Initialize();
IServiceContainer serviceContainer = this as IServiceContainer;
dte = serviceContainer.GetService(typeof(SDTE)) as DTE;
var solutionEvents = dte.Events.SolutionEvents;
solutionEvents.Opened += OnSolutionOpened;
var i = dte.Solution.Projects.Count; // Happy days !
}
上述所有代码行都预先存在solution VSPackage.cs内,但“var i = dte.Solution.Projects.Count;”除外我在VSPackage.cs行之后的44本地添加了。然后我打开solution,点击F5,在实验实例中,我打开了JoePublic.Sln,嘿,因为计数'2'正确 - 宾果!快乐的日子!