I am trying to write a add-in for Visual Studio that, among other things, needs to keep track of every file in a Visual Studio solution. I know what events I need to subscribe to (when a Solution is opened, when a file is added/removed/edited in it, the same for projects, etc), but I don't understand how to actually get a list of files from any of it.
I recently installed CodeRush and have been playing with the DXCore framework. I'm very happy with it's approach at plugins, but I still don't see an obvious way to get a list of files in the solution.
So to sum it up: How, via the Visual Studio SDK or DXCore, do I get a reliable list of files in the solution and it's projects?
A project is contained within a solution. Despite its name, a solution isn't an "answer". It's simply a container for one or more related projects, along with build information, Visual Studio window settings, and any miscellaneous files that aren't associated with a particular project.
In Visual Studio, click Tools > Options. Expand Projects and Solutions and click Locations. The Projects location field defines the default location for storing new projects. You can change this path if you are using a different working folder.
Explorer. The Explorer is used to browse, open, and manage all of the files and folders in your project. VS Code is file and folder based - you can get started immediately by opening a file or folder in VS Code. After opening a folder in VS Code, the contents of the folder are shown in the Explorer.
For example, you can right-click the project > click properties > select Application and see(refer to) the related information, for example, Output type -- Windows Application, Console Application, Class Library… Or you can find some related information about the project type from items, files etc.
Thanks, Reed; the article you linked got me far enough to get a proof of concept churned out in a couple minutes.
Since I feel it's related, here is the iteration and recursive means by which I collected the ProjectItems. I did this in DXCore, but the same idea applies to the raw Visual Studio SDK (DXCore is merely a nicer looking wrapper over the SDK). The 'Solution', 'Projects', 'Project', and 'ProjectItem' objects are right there in EnvDTE.
Setting Projects
EnvDTE.Solution solution = CodeRush.ApplicationObject.Solution;
EnvDTE.Projects projects = solution.Projects;
Iterating over the Projects to pull ProjectItems
var projects = myProjects.GetEnumerator();
while (projects.MoveNext())
{
var items = ((Project)projects.Current).ProjectItems.GetEnumerator();
while (items.MoveNext())
{
var item = (ProjectItem)items.Current;
//Recursion to get all ProjectItems
projectItems.Add(GetFiles(item));
}
}
Finally, The recursion I do for getting all ProjectItems in the active Solution
ProjectItem GetFiles(ProjectItem item)
{
//base case
if (item.ProjectItems == null)
return item;
var items = item.ProjectItems.GetEnumerator();
while (items.MoveNext())
{
var currentItem = (ProjectItem)items.Current;
projectItems.Add(GetFiles(currentItem));
}
return item;
}
This is all available easily using DTE in the Visual Studio SDK.
You can get a list of projects in a solution using the Projects interface.
You can get a list of items in a project using the ProjectItem interface.
For more information, I'd recommend reading up on Controlling Projects and Solutions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With