48

I'd like to create a new C# solution with Visual Studio Code.

I'm using yo tools.

Now I have a folder with ASP project. And another folder with class library.

How can I reference class library from asp project?

Seems I have to add dependency to the project.json:

"dependencies": {
    "ClassLibrar.Name": "*"
}

But to make this possible I need solution file. But VS Code and yo doesn't create a such one.

VS Code can be used from different OS, while VS accessible only from Windows, I'd like o create a new solution with VS Code only.

Simple requirement: create web project and 1+ class libraries.

All this will be packed in docker container

6
  • Sounds like you need to create a new project from within visual studio really. Explain what is the problem please?
    – Stuart
    Commented Mar 31, 2016 at 20:38
  • 3
    I'd like to create a new solution without VS, only VS Code
    – dr11
    Commented Mar 31, 2016 at 20:42
  • I use yeoman on mac, and was a matter of yo aspnet to get started. VS code is nowhere near VS proper, but it's great to be able to do real world c#.
    – Stuart
    Commented Apr 1, 2016 at 6:43
  • You mean that all your solution was located in 1 project and you didn't test it? Why did you minus my question? :-)
    – dr11
    Commented Apr 1, 2016 at 6:56
  • VS Code is just a (fully featured and excellent) text editor; it doesn't support solutions which are a Visual Studio-specific feature. Commented Apr 1, 2016 at 9:38

3 Answers 3

108

Open VS Code terminal and navigate to the directory where you want to create solution folder. Use following commands

dotnet new sln -o MyApiApp

The -o parameter lets you specify the output directory

Navigate to solution direction

Cd .\MyApiApp\ 

Create new projects under root solution folder

dotnet new console -o MyApiApp.ConsoleApp
dotnet new webapi -o MyApiApp.WebApi 
dotnet new classlib -o MyApiApp.Repository 
dotnet new xunit -o MyApiApp.Tests

Add projects to solution (use tab to navigate path).

dotnet sln MyApiApp.sln add .\MyApiApp.ConsoleApp\MyApiApp.ConsoleApp.csproj .\MyApiApp.WebApi\MyApiApp.WebApi.csproj .\MyApiApp.Repository\MyApiApp.Repository.csproj .\MyApiApp.Tests\MyApiApp.Tests.csproj 

Add project references

dotnet add .\MyApiApp.WebApi\MyApiApp.WebApi.csproj reference .\MyApiApp.Repository\MyApiApp.Repository.csproj 
dotnet add .\MyApiApp.ConsoleApp\MyApiApp.ConsoleApp.csproj reference .\MyApiApp.Repository\MyApiApp.Repository.csproj 
dotnet add .\MyApiApp.Tests\MyApiApp.Tests.csproj reference .\MyApiApp.WebApi\MyApiApp.WebApi.csproj .\MyApiApp.Repository\MyApiApp.Repository.csproj
1
  • 1
    Also in VS Code, if you have a complex nested folder structure, you can let VS Code do the folder path completion for you, by right clicking the csproj you would like to add. Then clicking copy relative path, then simply using dotnet sln add in the folder that contains the solution, then paste the relative path you copied :) saved me a lot of time.
    – David B
    Commented Jan 24, 2023 at 10:05
60

Visual Studio Code provides a way to create the new project templates.

  1. Navigate to Visual Studio Code terminal (press Ctrl+`)
  2. Type the command dotnet new sln
  3. You can also create the new project, check list of available project templates using command dotnet new list.

The detailed documentation is Available Here.

enter image description here

2
17

I find myself visiting this page, not for the first time, because I have already upvoted @Rahul-Uttarkar's answer.

Thing is, the answer is a solution using the terminal. It doesn't really leverage the fact that VS Code has a rich user interface, besides using the integrated terminal.

Things have moved on, there is a sweet little extension on the marketplace called vscode-solution-explorer that allows you to create a .sln file, add/remove new/existing projects and more. Behind the scenes it's making command line calls which you'll see if your terminal window is visible.

EDIT: to explicitly create a new solution file with this extension, you need to have VS Code point to an empty folder. Then the extension will say "No visual studio solution found learn more with two buttons below, "Open solution" and "Create New Solution"

3
  • 1
    I was about to create my own bash script to automatically create .sln files for me. It wasn't difficult, but this vscode-solution-explorer extension is SO good! Commented May 18, 2023 at 9:08
  • @user1007074 - I like that extension and it does good things with EXISTING .sln files. but I have not found (documenttion or empirically) the ability to create a NEW .sln [not using terminal] Commented Jan 5 at 11:42
  • 1
    @DavidV.Corbin thanks for taking the time to comment. Short answer is that, yes this extension can create new .sln files (the github readme has a little gif showing it). I have edited my answer to explain how to do it. I'll admit it's a little clunky, and it doesn't really allow for creation of multiple solution files in the same locaiton. Commented Mar 11 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.