DotNetCore

Manage and Edit soluton files with visual studio code and .net CLI




Let’s take a look at creating Visual Studio Solution Files in .net Core, using the Command Line Interface (CLI).

Although I will be predominantly using the ubuntu operating system, it really doesn’t matter which operating system you use, the steps are the same irrespective of platform. In fact, you don’t even need to use Visual Studio Code to follow along, as long as you have access to .net CLI you will be able to accomplish this task.

What are Solution files

If you have worked with the .net framework for any length of time, you will be well acquainted with Solution Files, which contain the text-based information your development environment will use to load the name-value parameters of all the projects that comprise a software solution.

Each project file will contain additional information read by the environment to populate the hierarchy of the specific project.

The information contained in a solution file is not only used by the IDE,  it is also used my MsBuild, Cake Build or any other build tool to build or compile the solution.

A solution is a structure for organising projects in Visual Studio. The solution maintains the state information for projects in text based .sln

Do we need Solution files in VS Code ?

Although not strictly necessary from a pure development perspective, we don’t necessarily need solution files when developing with VS Code, but solution files do come in handy during the Build and Release phases of projects, so it is generally good practice to start thinking about using them in the early stages of your project development.

The latest release of the .net core framework, has seen a number of changes incorporated in regards to Development Project Management.  We’ve seen the demise of the project.json and the re-introduction of .csproj files.   We have also see the introduction the of creating Solution Files in the .net CLI

What are Project Files

Project Files a.k.a .csproj contains of all the source code files, icons, images, data files and anything else that will be compiled into an executable program or web site, or else is needed in order to perform the compilation.

A project also contains all the compiler settings and other configuration files that might be needed by various services or components that your program will communicate with.

A project is an XML file (*.csproj ) that defines a virtual folder hierarchy along with paths to all the items it “contains” and all the build settings.

In Visual Studio, the project file is used by Solution Explorer to display the project contents and settings. When you compile your project, the MSBuild engine consumes the project file to create the executable. You can also customise projects to product other kinds of output.

 

Creating a new solution file

It is now really easy to create solution file making use of dotnet new.

To create a new solution file in your current working directory :

This works great! However, typically you may want some additional control of the naming etc of your solution file. You can do this by making use of the command line arguments.

To get a full list of the available arguments simply use the help function

In order to create a new solution file with the name of SwcApi we simplay provide the name parameter

Your new solution file is now created and you can open and inspect the file in VS Code. Your Solution file is of no use until you start adding projects to it. so lets go ahead and some projects to it to start off.



Adding Projects to solution files

In my example I am going to add 2 new projects then I will add them to the Solution file. We’ll create an Web Api project and simple Class Library project.

We can now add these projects to our solution file using the dotnet sln [path-to-solution.sln] add [path-to-project.csproj]

You’ll see a confirmation message. You have npow successfully added projects to your solution file.

Removing projects from solution files

Removing projects files using the CLI is also fairly trivial process. Making use of the remove command i.e. dotnet sln remove [path-to-project.csproj]

List Projects

.NET CLI has the ability to list the projects in the solution file, instead of having to open it up and wade through the litany of GUIDs, which is especially handy when you working on a headless linux build server!

dotnet sln list

Summary

Making use of the .net core cli you can manage your solution files with ease, without having to edit the file by hand. It really is the worth the time and effort learning how to make use of the solution files, as it does drastically reduce the effort required in building your project later!



Additional References

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
π