4

I have a list of Project GUIDs from a sln file.

I need to find the project corresponding to each GUID programatically.

How can I do this?

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
aromore
  • 977
  • 2
  • 10
  • 25
  • 1
    Is your program running in-process with Visual Studio? as a package? as an add-in? What object do you already have? – Simon Mourier Sep 10 '12 at 14:49
  • I am trying to extract the project dependencies from an sln file. I parsed the sln file for the same, and got a list of Project GUIDs, because that is how the dependencies are mentioned in the sln file. Now, I want to get the project names/paths of all the GUIDs I have and write them to a text file. Does that answer your qn? – aromore Sep 10 '12 at 14:58

3 Answers3

3

Project GUIDs are stored along side the project name inside of the .sln file like so:

Project("{Project Type GUID}") = "MyProject",
"Source\MyProject\MyProject.vbproj",
"{AB5BA87B-77D5-4812-B02C-9B4FB87F4EF6}" EndProject

Theoretically you could preparse the solution file and build up a map of Project-To-GUID associations. Once you have that map established, you should be able to quite easily swap out any references to a project's GUID with the actual project name.

Note: the first GUID you see in the project file is for the project type, NOT the actual project. This helps Visual Studio manage what template to use when opening and working with the file. The one you want in this case is {AB5BA87B-77D5-4812-B02C-9B4FB87F4EF6}.

mclark1129
  • 7,532
  • 5
  • 48
  • 84
1

All Visual Studio setup resides in the registry, in a key like this:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\[Version]

Version can be like "9.0" (VS 2008), "10.0" (VS 2010), "11.0" (VS 2012), but it can also be anything else because Visual Studio can be started with a custom 'hive' key (see the /RootSuffix parameter of the devenv.exe command line).

So, for normal operation, project guids are located here:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\[Version]\Projects

Projects in Visual Studio can be standard projects (C#, VB.NET, etc.) or aggregate projects (the WPF "flavor" over C# or VB.NET).

In general, projects have a "Package" key that contains the guid of the Visual Studio Package (the binaries) containing the type of project.

Packages descriptions are to be found here:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\[Version]\Packages

Native packages (C#, VB.NET, C++, ...) are implemented as native COM servers and the path is defined in the InprocServer32 key. Managed packages define MSCOREE.DLL as the InprocServer32 key and there is also a CodeBase key is the package is not in the GAC and only an Assembly key if the package is in the GAC.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • yes. I get that. But I need to get the correspnding Project name programatically. Is there a way to do that? – aromore Sep 10 '12 at 15:14
  • @aromore - Some project types (C#,VB.NET) store projects configuration as XML, so you can open these files (csproj, vbproj) and get the information from there, if that's what you're after. – Simon Mourier Sep 10 '12 at 15:20
1

I may be very late to answer this question, but in the hopes of helping any other person who wonders over this question. There are several assemblies that you would like to use for parsing solution and project files in C#, they are:

Microsoft.Build.Evaluation

and

Microsoft.Build.Construction

With in the later assembly there is a class called SolutionFile. Documentation. Within the documentation SolutionFile contains the property ProjectsByGuid, on which you can do a linq query on if you already have the GUID. The code to do this would look a little like this:

var guid = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
var pathToSolutionFile = "C:\users\user\source\repos\solution\solution.sln"
var sln = SolutionFile.Parse(pathToSolutionFile);
var projectName = sln.ProjectsByGuid.Where(x => x.key==guid)
                     .FirstOrDefault().Value?.projectname;

I hope this helps you, or anyone else.

Dennis.Verweij
  • 116
  • 1
  • 9