Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile and run a C# code in VSCode (Linux Ubuntu)?

I'm really struggling trying to compile and run a simple C# "Hello World!" code. Typing "dotnet new" in the terminal does nothing and typing "dotnet new console" simply creates a .csproj file, a "obj" folder and a .cs file (a "Hello World!" code), which I still can't run it. I've already downloaded SDK for linux and the VSCode extensions "C#" and "Code Runner".

When I try using Ctrl+Alt+N the output tab shows "/bin/sh: 1: scriptcs: not found".

like image 255
O Tal do Juca Avatar asked Jan 01 '23 06:01

O Tal do Juca


1 Answers

had same problem, here are my steps:

Considering of course you already installed VSCode and dotnet framework.

-Create New Folder somewhere on your system and rename it to something you like "Test" or what ever you like.

-Start VS code and open this folder (File->Open Folder)

-Open new terminal (Terminal-> New Terminal) and type dotnet new console, and wait until its done

-then type dotnet restore && dotnet build, and wait until its done

-on the right side of VS Code you should see files appearing.

-open Program.cs and press Ctrl+F5, when prompt click open "launch.json"

-In launch.json file scroll down until you see line:

"program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",

On this line of code change <insert-target-framework-here> to what ever is written after bin/Debug/ ,in my case its netcoreapp3.1, if you can't see it, click on /bin on right, it should show.

And change <insert-project-name-here> to your project name in my case its Test, if you find problem with this, it's the name of only .dll file in /bin folder (if your folder is all caps like TEST this file will be named Test.dll for unknown reasons to me)

when all is done this line should look something like

"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/Test.dll",

-Save and close launch.json

-Open Project.cs file and run again with Ctrl+F5

-When prompt click "Configure Task" on top of work screen you will see something like "Configure task.json from template", click on that, then click on ".NetCore" on next drop down list.

this will create task.json file

-Save and close task.json.

-Return to Project.cs and run it again. you should see in terminal "Hello world".

-Now start creating your own code :D

Sorry for lengthy explanation, but this worked for me.

like image 170
Kemal Musaefendic Avatar answered Jan 02 '23 21:01

Kemal Musaefendic