Frankly speaking “several” keyword in the title is an overuse, because while writing this article I’m thinking only about two versions of dotnet CLI. Why? Because RC4 version dropped support to project.json and switched completely to the new *.csproj format. On one hand it’s good – let’s follow the hype, cool to have tooling already. On the other hand if you still maintain/develop solution based on project.json then you have a problem (you will not be able to use dotnet CLI with that).
I am in such case and have found a solution that might be worth sharing.
Download dotnet CLI binaries (without installer) and extract package to some known folder. In my case it was:
Overriding PATH variable
The trick is to switch PATH variable to proper dotnet CLI location. Of course you could copy paste specific “set PATH=…” expression but some script may come in handy. Below is the script that works for me (I use an alias “set-dotnet.cmd”). Remember to add script location to PATH (you may want to see this post).
@echo off set _DOTNET2=c:\Tools\dotnet-prev2; set _DOTNET4=c:\Tools\dotnet-rc4; :: Batch way of string replace set path=%path:C:\Tools\dotnet-prev2;=% set path=%path:C:\Tools\dotnet-rc4;=% if [%1]== goto usage if "%1"=="prev2" goto prev2 if "%1"=="rc4" goto rc4 goto unknown :prev2 @echo you selected prev2 set path=%_DOTNET2%%PATH% exit /B 0 :rc4 @echo you selected rc4 set path=%_DOTNET4%%PATH% exit /B 0 :unknown @echo Unknown parameter %1 goto :usage :usage @echo Usage: %0 ^ @echo where ^ is one of: prev2, rc4 exit /B 1
This lets me switch PATH to specific dotnet version using:
set-dotnet prev2 set-dotnet rc4
Normally I do scripting in Powershell but things related to environment variables are done easier in batch scripting – neither setx nor [Environment]::SetEvironmentVariable() affect currently active console (challenge me in comments if you want).
As batch scripting is pretty exotic some explanation is needed:
Exit /b 0
This means exiting current subrouting which is the whole batch script in case of “goto” or returning from “call” (0 is errorlevel)
:: Batch way of string replace %variable:findtext=replacewith%
This is a way to do string replace withing variable. Let’s have another example
set x=hello world set x=%x:world=chriss%
Executing these two lines above results in x=hello chriss
Further refactoring can lead to having much simpler version which is having two batch scripts:
As an extra credit this will enable autocomplete for set-dotnet-*