Question

Does VS have an environment variable for the path to the current solution?

For example, there is "%PathToWebRoot%" which is generally set to the "WebSites" directory in your VS project directory. The value of it can be changed in any number of ways. I'm wondering if there's a variable that changes each time you load a solution to point to it's root.

I'm creating unit tests that will potentially be run on different machines and one of the directions you have to set before the method is the AspNetDevelopmentServerHost. It's recommended not to hard code them, of course, but the recommended environment variable isn't necessarily where your site will be and isn't in this case.

Was it helpful?

Solution

VS has a lot of Macro variables that work from within VS. The one that gives you the full path to the solution is called $(SolutionDir). However, while this works from the Pre- and Post-build events (and, AFAIK, in the solution's MSBuild file), this is not an environment variable. If you think about, you can't have a (global) environment variable that points to "the path to the current solution", since you may have more than one instance of VS open.

Depending on your unit testing framework, you can often ask it about its execution context to get the current directory. That may not give you the solution path, though...

OTHER TIPS

Visual Studio does expose an environment variable for the solution path - it's called SolutionPath. It looks like this:

SolutionPath=D:\Team\MyProject\main-branch\MyProject.sln

Importantly, you can use it in any process spawned from Visual Studio - for example, launching batch scripts from your solution (that's how it can be different across different instances of Visual Studio).

If you configure Visual Studio to launch .bat files by double clicking on them by following this guide, then the environment variable will be available in your batch file.

In my particular case, I needed to get to a directory within my solution and I had a few branches of code all at the same level. But my batch script was at a higher level in the source tree and it had no easy way of determining the 'current' branch. For example, this was my structure:

D:\Team\MyProject\
  L-- Build
      L-- MyBatchScript.bat
  L-- main-branch
      |-- MyProject.sln
      L-- important-folder
  L-- dev-branch
      |-- MyProject.sln
      L-- important-folder

I referenced MyBatchScript.bat as a solution item in each solution so I could launch it by double clicking on it.

To get the folder of the solution rather than the path to the solution file, I used a bit of DOS batch script magic:

FOR %%d IN (%SolutionPath%) DO SET SolutionFolder=%%~dpd
REM Now I can run a tool with the correct folder
mycustomtool.exe %SolutionFolder%important-folder
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top