52

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? Commented Mar 31, 2016 at 20:38
  • 3
    I'd like to create a new solution without VS, only VS Code 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#. 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? :-) 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

4 Answers 4

116

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
62

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 Comments

stackoverflow.com/a/42730946/908677 you can add projects to the solution using syntax like: dotnet sln add .\Foo\Foo.csproj
Those capabilities are provided by .Net - not by Vs Code. Even ht eC# DevKit [marketplace.visualstudio.com/… does not seem to have this capability.
20

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 Comments

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!
@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]
@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.
0

If you want to create only the solution file for your exsisting project in VS code then use command
dotnet new sln --name yourProjectName

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.