Does VS have an environment variable for the path to the current solution?
VS是否有环境变量用于当前解决方案的路径?
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.
例如,存在“%PathToWebRoot%”,它通常设置为VS项目目录中的“WebSites”目录。它的价值可以通过多种方式改变。我想知道每次加载解决方案指向它的根时是否有变量。
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.
我正在创建可能在不同机器上运行的单元测试,以及在方法是AspNetDevelopmentServerHost之前必须设置的方向之一。当然,建议不要对它们进行硬编码,但建议的环境变量不一定是您的站点所在的位置,在这种情况下也不一定。
2 个解决方案
#1
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.
VS有很多可以在VS内工作的宏变量。为您提供解决方案的完整路径的那个称为$(SolutionDir)。但是,虽然这适用于构建前和构建后事件(以及AFAIK,在解决方案的MSBuild文件中),但这不是环境变量。如果您考虑一下,您不能拥有指向“当前解决方案的路径”的(全局)环境变量,因为您可能有多个VS实例打开。
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...
根据您的单元测试框架,您可以经常询问它的执行上下文以获取当前目录。这可能不会给你解决方案路径,但......
#2
Visual Studio does expose an environment variable for the solution path - it's called SolutionPath
. It looks like this:
Visual Studio确实公开了解决方案路径的环境变量 - 它被称为SolutionPath。它看起来像这样:
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).
重要的是,您可以在从Visual Studio中生成的任何进程中使用它 - 例如,从您的解决方案启动批处理脚本(这是不同的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.
如果将Visual Studio配置为通过按照本指南双击它们来启动.bat文件,则环境变量将在批处理文件中可用。
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.
我在每个解决方案中都引用了MyBatchScript.bat作为解决方案项,因此我可以通过双击它来启动它。
To get the folder of the solution rather than the path to the solution file, I used a bit of DOS batch script magic:
要获取解决方案的文件夹而不是解决方案文件的路径,我使用了一些DOS批处理脚本魔术:
FOR %%d IN (%SolutionPath%) DO SET SolutionFolder=%%~dpd
REM Now I can run a tool with the correct folder
mycustomtool.exe %SolutionFolder%important-folder
#1
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.
VS有很多可以在VS内工作的宏变量。为您提供解决方案的完整路径的那个称为$(SolutionDir)。但是,虽然这适用于构建前和构建后事件(以及AFAIK,在解决方案的MSBuild文件中),但这不是环境变量。如果您考虑一下,您不能拥有指向“当前解决方案的路径”的(全局)环境变量,因为您可能有多个VS实例打开。
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...
根据您的单元测试框架,您可以经常询问它的执行上下文以获取当前目录。这可能不会给你解决方案路径,但......
#2
Visual Studio does expose an environment variable for the solution path - it's called SolutionPath
. It looks like this:
Visual Studio确实公开了解决方案路径的环境变量 - 它被称为SolutionPath。它看起来像这样:
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).
重要的是,您可以在从Visual Studio中生成的任何进程中使用它 - 例如,从您的解决方案启动批处理脚本(这是不同的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.
如果将Visual Studio配置为通过按照本指南双击它们来启动.bat文件,则环境变量将在批处理文件中可用。
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.
我在每个解决方案中都引用了MyBatchScript.bat作为解决方案项,因此我可以通过双击它来启动它。
To get the folder of the solution rather than the path to the solution file, I used a bit of DOS batch script magic:
要获取解决方案的文件夹而不是解决方案文件的路径,我使用了一些DOS批处理脚本魔术:
FOR %%d IN (%SolutionPath%) DO SET SolutionFolder=%%~dpd
REM Now I can run a tool with the correct folder
mycustomtool.exe %SolutionFolder%important-folder