A project with that name is already opened in the solution

So this happened! Visual Studio shows this error:

A project with that name is already opened in the solution.

Why?

It happended to me, when I modified a Visual Studio Project File (*.csproj) from outside Visual Studio (VS). So what I did, it was creation of a Microsoft.Build.Evaluation.Project instance pointing at my project, which was currently opened in Visual Studio. But let’s try to start from the beginning.

Let’s suppose, we have MyProject.csproj opened in VS.

Then, from outer executable (outside VS), we have another program, in which we call (C#):

using Microsoft.Build.Evaluation;

Project p =
 new Microsoft.Build.Evaluation.Project("$path$\Myproject.csproj");

This line creates an instance of a Project class (pointing to MyProject.csproj file) but in the same time, it locks the file. So, if next we modify this project, and we save it, by calling

p.Save();

our VS is saying:

If we choose Reload, this may happen:

Error message: A project with that name is already opened in the solution.

At first, I couldn’t know, what it is about. Finally, I figured it out, that MyProject.csproj file is locked, and VS cannot reload it.

What should I do then?

The answer was easy, although I needed to search for it. It seems, that if you create a Project instance, it is added to  Microsoft.Build.Evaluation.ProjectCollection, and in the same time, it locks the csproj file. So it is enough to call (in the same outer program, after we finish work with p variable)

using Microsoft.Build.Evaluation;

ProjectCollection.GlobalProjectCollection.UnloadProject(p);

And all cames back to normal. VS does not have any problem with reloading MyProject.csproj anymore.

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>