Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble running C# code in VS Code: Getting scriptcs error

This is my first time using Visual Studio Code and I am trying to run a simple code on it but it is giving me an error that says:

'scriptcs' is not recognized as an internal or external command, operable program or batch file.

I have this as code:

using System;

struct Employee{
   public int Id { get; set; }
   public string FullName { get; set; }
}

public class MyClass{
 public static void Main(){
   Employee obj= new Employee();
   obj.FullName = "Hello World";
   Console.WriteLine(obj.FullName);
  }
}

I have tried installing the Scriptcs Runner, but still the same issue. Can anybody suggest something else?

Edit:

After many of your suggestions I tried the following:

  • Created an entire new folder without spaces and named the files the same way.
  • Re-installed the scriptcs Runner extension from VS Code Extension manager.
  • Doubled checked my code. It is running on online c# compiler, but not in VS Code.

Problem is still the same. Getting the above error.

like image 402
noobprogrammer Avatar asked Dec 10 '19 06:12

noobprogrammer


3 Answers

  1. Open the Extensions and install the C# extension: Name: C#, ,Id: ms-vscode.csharp ,Description: C# for Visual Studio Code (powered by OmniSharp). ,Publisher: Microsoft ,VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp

  2. Go to console into a folder of your choice and create a new console project by typing dotnet new console -o TestConsoleApp This will create a project file TestConsoleApp.csproj

  3. Start VS Code by typing Code .

  4. In VS Code goto Terminal and execute dotnet run --project TestConsoleApp to run your application. Alternatively you can start Debugging (F5)

This should provide you a good start where you then can use your code.

If you just want to use code snippets, you should try what Athanasios Kataras answered but with that I have no experience.

There is a also video introduction for this available at https://channel9.msdn.com/Blogs/dotnet/Get-started-VSCode-Csharp-NET-Core-Windows

like image 71
Mihaeru Avatar answered Sep 19 '22 21:09

Mihaeru


You need both the script runner extension and to install scripts

Installation and information guide is here: http://scriptcs.net/

  1. Install chocolatey
  2. Run cinst scriptcs to install the latest version.

Then make sure you install the script runner extension as per this guide: https://www.strathweb.com/2015/11/running-c-scripts-and-snippets-in-visual-studio-code-with-scriptcs/

The extension can be installed directly from VS Code:

  1. press F1
  2. type ext install scriptcsRunner
  3. choose “install”
like image 27
Athanasios Kataras Avatar answered Sep 20 '22 21:09

Athanasios Kataras


You're attempting to run a csharp program without creating it as a project. For simple one-offs that you would like to run quickly without creating a full project, you need the c# script runner. You can install it with the following instructions. Below is a link to the extension's github page for reference.

Go to Extensions, search for scriptcs and install scriptcsRunner. You mention that you have this installed, but it should be all you need for a simple one file program like you have above. Double check that it is the version here: https://github.com/filipw/vscode-scriptcs-runner.git

Alternatively, if you want to create an actual project, you will need to run a few commands within the integrated terminal.

  • Initialize the Project
    • dotnet new console
  • Add your code
  • Run through VSCode
    • or type dotnet run in the terminal

https://code.visualstudio.com/docs/languages/dotnet

like image 31
Cryolithic Avatar answered Sep 19 '22 21:09

Cryolithic