Pages

Visual Studio Code Solution creation on Linux Mint

Creating a C# based Solution in Visual Studio Code on Linux isn't as straightforward as using the full version of Visual Studio 2017 on Windows.

The steps below detail how to create a Solution with a Console project and an MSTest project for Unit Testing.

Create a folder for the whole solution and create the solution (mySolution):
mkdir mySolution
cd mySolution
dotnet new sln
Create a folder for the main project, then create the main project (myProject):
mkdir myProject
cd myProject
dotnet new console
Create a folder for the test project, then create the test project (myTests):
cd ..
mkdir myTests
cd myTests
dotnet new mstest
Switch back to the solution folder and add the main project and test project into the solution file (mySolution.sln, myProject.csproj, myTests.csproj):
cd ..
dotnet sln mySolution.sln add myProject/myProject.csproj myTests/myTests.csproj
Switch to the tests project folder and add a Reference to the main project (myTests, myProject):
cd myTests
dotnet add reference ../myProject/myProject.csproj
Switch back to the solution folder and run the tests (myTests):
cd ..
dotnet test myTests/myTests.csproj

Open the mySolution folder in Visual Studio Code and the Explorer view should look like this:


And in Bash script form:
#!/bin/sh
#$1 is the solution name
#$2 is the main project name
#$3 is the main project type e.g. console, classlib, web etc.

echo "Create a folder for the whole solution and create the solution"
mkdir $1
cd $1
dotnet new sln

echo "Create a folder for the main project, then create the main project"
mkdir $2
cd $2
dotnet new $3

echo "Create a folder for the test project, then create the test project"
cd ..
mkdir $2Tests
cd $2Tests
dotnet new mstest

echo "Switch back to the solution folder and add the main project and test project into the solution file"
cd ..
dotnet sln $1.sln add $2/$2.csproj $2Tests/$2Tests.csproj

echo "Switch to the tests project folder and add a Reference to the main project"
cd $2Tests
dotnet add reference ../$2/$2.csproj

echo "Switch back to the solution folder and run the tests"
cd ..
dotnet test $2Tests/$2Tests.csproj

For example:
./CreateVsCodeSolution.sh mySol2 myProj2 console

will create this solution: