Visual Studio 2010 Add-in: How to locate project references? - visual-studio-2010

How do you programmatically find & iterate all project & dll references within a Visual Studio 2010 solution?
I can iterate all projects and found the Project.ProjectItems property and Project.Properties but have not found any way to reference the references (pun intended).
This is happening in an add-in, so a DTE solution is preferable to anyone suggesting we iterate the files.
Proposed solution based on the answers below:
You need to find and include a reference to VSLangProj.dll (e.g. in Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies)
Then you can iterate all selected project's project & DLL references like this:
foreach (Project project in (object[])_applicationObject.ActiveSolutionProjects)
{
VSProject vsProject = project.Object as VSProject;
if (vsProject != null)
{
foreach (Reference reference in vsProject.References)
{
// Do cool stuff here
}
}
}
Info for Tomas Lycken:
_applicationObject is a private member in my add-in, e.g.
private DTE2 _applicationObject;
I set it in the connection like this:
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;

You can access the VB/C# specific VSProject object from the Object property of the Project object. VSProject has a References property, through which you can drill down to the individual references.
It has to be this way, if you think about it, since not all projects loaded in visual studio will necessarily support references to .NET assemblies, so it has to be something specialized for C#/VB (and other .NET languages), rather than off of the core Project object.

C# and VB projects have an 'Object' property which you can cast into a VSProject, from which you can access the references. There's sample VB code on those pages.

Related

Can I make a windows form application in Microsoft Visual Studio Express 2013 Windows 8?

Seems likely that I am not able to create a windows form application using the Microsoft Visual Studio Express 2013 under Windows 8. The default templaters are only for creating apps rather than traditional normal windowed applications.
Is there any way I could find the template or find a solution?
Thanks.
You cannot.
Download Microsoft Visual Studio 2013 Express for Windows Desktop instead.
Link for online installer
Link for ISO file
Visual Studio download page
Actually yes, you can do that manually, by adding the references to the project (System.Windows.Forms) and changing the output type in the properties of the project, and adding classes - you have to manually do the whole thing..
To Expand RobDev's answer, you can actually do this. I will show you an example of how to do this in Visual Studio Express 2015 for Web, probably same applies for 2013 version.
Steps.:
Create an empty solution
Create a .dll library project
Change output in project properties to Windows Application. To do so, right click on the new added project and pick "Properties". There, you can change "Output Type" dropdown in Application tab.
Add a reference to System.Windows.Forms to your project (by right-clicking => Add Reference to your Reference option under your project)
Set the project to Startup Project, by right-clicking the project and choosing "Set up as Startup Project"
add a main method to the generated class (you can rename it to Main). It will need this code to start.
using System.Windows.Forms;
namespace ClassLibrary1
{
public class Class1
{
public static void Main()
{
Application.Run(new Form());
}
}
}
and voila! That will create a new fresh Form. You could use a starting point whatever form you want. To do so, add a new .cs class and make it inherit from Form class.
The toolbox to customize forms is available at View => Toolbox menu.

Determine project type ( ProjectTypeGuids ) in VS2010 programmatically for Sharepoint Projects

I use VS2010 and Sharepoint 2010 projects.
I have an VS 2010 Addin, and I need detect type project programmatically.
I edit my csproj (for Sharepoint Project), and I have seen this ProjectTypeGuids:
{BB1F664B-9266-4fd6-B973-E1E44974B511};{14822709-B5A1-4724-98CA-57A101D1B079};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
Any FULL list of known project type Guids, included Sharepoint projects?
How can I get ProjectTypeGuids programmatically for my EnvDTE.Project in C# ?
References:
List of known project type Guids
http://www.mztools.com/articles/2008/mz2008017.aspx
What is the significance of ProjectTypeGuids tag in the visual studio project file
http://social.msdn.microsoft.com/Forums/en/vsx/thread/d9d05cdc-96a1-4044-95d8-a4f8885a660a
What is the significance of ProjectTypeGuids tag in the visual studio project file
http://www.mztools.com/articles/2007/MZ2007016.aspx
Carlos J.Quintero:
See:
HOWTO: Get the project flavor (subtype) of a Visual Studio project from an add-in
http://www.mztools.com/Articles/2007/MZ2007016.aspx
There is no full list since everyone can create new project types, that's the reason GUIDs are used.
You can create SharePoint 2010 projects and get the Guids, that's how I got that list.
Mathias Herbaux:
If you can get IVsSolution instance, you can have all GUID that compose your project:
public bool IsTypedProject(IVsSolution solution, EnvDTE.Project project)
{
IVsHierarchy hierarchy;
solution.GetProjectOfUniqueName(project.UniqueName, out hierarchy);
IVsAggregatableProjectCorrected AP;
AP = hierarchy as IVsAggregatableProjectCorrected;
string projTypeGuids = string.Empty;
if (AP != null)
{
AP.GetAggregateProjectTypeGuids(out projTypeGuids);
if (projTypeGuids.ToUpper().Contains("YOUR_GUID".ToUpper()))
{
return true;
}
}
return false;
}
Maybe you can can request service like this :
(IVsSolution)ServiceProvider.GetService(typeof(SVsSolution))
References:
http://social.msdn.microsoft.com/Forums/da-DK/vsx/thread/bf18037e-479a-4ca8-ae64-81ee39652d73

Get a list of projects/references within a Visual Studio 2010 Extension

I'm creating a Visual Studio 2010 extension package (Microsoft.VisualStudio.Shell.Package) that needs to analyze all of the projects and those project's references. I would assume that this is done with a service (e.g. Package.GetService(typeof(IMenuCommandService))). What I need is the interface that contains the functionality to get a list of projects and references for those projects. Also, any advice on where to find a reference that contains the available interfaces within visual studio would be much appreciated.
Note that I've seen multiple people trying to do something similar using DTE from a macro. That's not what I'm trying to do. I'm trying to do the same thing from within a Visual Studio Extension.
So even though you're doing this as part of an extension, you'll still need to use the DTE APIs to get all of the information you want. It may seem backwards but that's just how it works. You should grab the DTE object via (EnvDTE.DTE)Package.GetService(typeof(SDTE)). Once you have a EnvDTE.Project, access it's Object member and cast that to a VSLangProj.VSProject if it's a C# or VB project. This has the reference information you need.

VS2010: Going to definition for a C# member in a VB.NET project (and vice versa)

I have a solution with a C# project and a VB.NET project. The VB.NET project references the C# project. When I am in the VB.NET project and want to go to definition of an object member located in the C# project, VS takes me to the object browser instead of the member. The same occurs when I am doing the opposite in the C# project. What I would like it to do is expose its member to me.
Note: I am referencing the actual project and not the DLL.
This is a known limitation of Visual Studio. The corresponding Connect issue was closed as "By Design": http://connect.microsoft.com/VisualStudio/feedback/details/349633/go-to-definition-goes-to-object-browser-when-type-is-written-in-c-and-current-code-is-vb

How to create a F# based Visual Studio Add-In?

Even with F# installed, Visual Studio 2008 (and probably 2010) only provides Add-In project templates for C#, VB.NET and C++.
So, how to create a F# based Add-In?
I have tried to create a F# class library project with a new class implementing IDTExtensibility2, setting the correct references (EnvDTE, Extensibility, ...), copying a few project attributes from the C# Add-In (mainly the command line for debugging), and creating the .AddIn manifest file by hand, but no success. When VS is launched, my Add-In is not listed in the available ones.
What are the missing steps? Some kind of COM registration somewhere?
I've not tried this exact scenario, but the general strategy I've had the best luck with is: start with the C# template, rename the .csproj to .fsproj and change the <Import>s for F# and replace the <Compile> of C# code with F# code, and go from there.
The templates often have various important logic in them for build/deployment, so start with a working template for C# and tweak it for F#.
I finally found what went wrong: I simply forgot to put the .AddIn file in the AddIn directory (the C# wizard is doing this automatically).
So, to create a F# based Add-In for Visual Studio:
Create a new F# class library project
Add a few reference assemblies to the project (mainly EnvDTE, EnvDTE80, EnvDTE90, Extensibility)
Create a new class implementing IDTExtensibility2
Copy the debugging properties of a C# based Add-In project into your F# project
Copy the .AddIn manifest file, and modify it according to your F# project
*.. and don't forget to copy that new file in the AddIn directory for the user session
enjoy!

Resources