Creating projects and solutions using the dotnet command
I've been digging into .NET Core a little bit more recently, with the intention of using it for an upcoming project at work (sssh, don't tell anyone - they don't know yet!) and part of that has been filling in the gaps in my knowledge. I know I can create a solution and a project via Visual Studio, but the dotnet command exists so it's really not a bad idea to have an understanding of how it can be used to achieve the same outcome!
It turns out that the commands that I need to know to create a new solution, a project and then combine the two are:
- dotnet new sln
- dotnet new classlib (as I'm creating a class library)
- dotnet sln add
So, what I did was created a new solution, created a project and then added the project to the solution. The solution and the project were both named very imaginately as 'RobertWray.NewProject'. The steps to do this are:
- Create the directory to host your solution and cd into that directory
- Run the command dotnet new sln to create the solution file - it will be named the same as the containing folder
- Create the directory to host the project and cd into that directory
- Run the command dotnet new classlib to create the claas librar - again, it will be named the same as the containg folder
- cd up back into the directory that contains the solution file
- Run the command dotnet sln add .\RobertWray.NewProject\RobertWray.NewProject.csproj (substituting the name of your project)
That's all there is to it. One solution that contains a project, created via the command line!
Just because it might be useful, here's the output from running the commands in order in PowerShell:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS D:\> md RobertWray.NewProject
Directory: D:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 16/06/2018 18:41 RobertWray.NewProject
PS D:\> cd .\RobertWray.NewProject\
PS D:\RobertWray.NewProject> dotnet new sln
The template "Solution File" was created successfully.
PS D:\RobertWray.NewProject> md RobertWray.NewProject
Directory: D:\RobertWray.NewProject
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 16/06/2018 18:50 RobertWray.NewProject
PS D:\RobertWray.NewProject> cd .\RobertWray.NewProject\
PS D:\RobertWray.NewProject\RobertWray.NewProject> dotnet new classlib
The template "Class library" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on D:\RobertWray.NewProject\RobertWray.NewProject\RobertWray.NewProject.csproj...
Restoring packages for D:\RobertWray.NewProject\RobertWray.NewProject\RobertWray.NewProject.csproj...
Generating MSBuild file D:\RobertWray.NewProject\RobertWray.NewProject\obj\RobertWray.NewProject.csproj.nuget.g.props.
Generating MSBuild file D:\RobertWray.NewProject\RobertWray.NewProject\obj\RobertWray.NewProject.csproj.nuget.g.targets.
Restore completed in 151.53 ms for D:\RobertWray.NewProject\RobertWray.NewProject\RobertWray.NewProject.csproj.
Restore succeeded.
PS D:\RobertWray.NewProject\RobertWray.NewProject> cd..
PS D:\RobertWray.NewProject> dotnet sln add .\RobertWray.NewProject\RobertWray.NewProject.csproj
Project `RobertWray.NewProject\RobertWray.NewProject.csproj` added to the solution.
PS D:\RobertWray.NewProject>