How to "get rid of" $(SolutionDir) when building Visual Studio project from outside Visual Studio

You can set the variable by passing parameter arguments:

/p:SolutionDir=path

So, rather than editing the solution file, you can create a build script that sets up the environment and executes MSBuild accordingly, leaving the Visual Studio file as is for development work.


You can use a relative path.

I am not sure about Visual Studio 2010 as in the question, but it guaranteed works since Visual Studio 2012.

If the project folder is right under the solution folder then the relative path is ..\\.

This, by the way, can be combined with the parameter argument from Grant Thomas's answer.

<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>

The solution to explicitly pass SolutionDir as an argument is insufficient for me.

Namely, if the project I want to build has dependencies with different build configurations, the build fails with a This project doesn't contain the Configuration and Platform combination of <my target configuration> error.

This makes sense, since I didn't specify the target configuration for the dependencies and thus it tries to target same configuration as the project I'm building, which they don't have (in my case).

Since a solution file contains the build configurations for all projects in the solution (given some solution-configuration), we want to make use of this solution to solve this problem. Luckily, MSBuild allows us to build a solution and pass a specific target project as argument, which will only cause that project to be build (together with its dependencies).

msbuild MySolution.sln /t:MyProject