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

69k Views Asked by At

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.

11

There are 11 best solutions below

4
On BEST ANSWER
  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

2
On

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

2
On

This issue can be solved by changing the command that is executed by code runner within your terminal, this has been beautifully shown here in this video.

1
On

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”
2
On

the problem is not in VS Code. the problem is that your code is incorrect. the line with the output to the console should be in another area, for example, in the constructor.

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

public class MyClass
{
    Employee obj = new Employee();

    public MyClass()
    {
            Console.WriteLine(obj.FullName);
    }
}
1
On

hi i'm newbie in programming field and so in C# but i had an error like this and it was cause of the way i run codes. i used the tempcoderunnerfile to run c# and it was the reason i got that error about scriptcs.

My Error

pictur of tempcoderunnerfile

Don't use this way

so i removed the tempcoderunnerfile and use the cmd (dotnet run) to run the code and it has successfully run the code

like this: use dotnet run

0
On

In VS code terminal write the below one line code and press Enter:

dotnet run

0
On

Assuming this is because you are using the code-runner extension:

  1. (If you haven't already got a .csproj file - initialize the project by running dotnet new console in the terminal in the same folder as your code)
  2. File>Preferences>Settings
  3. Here on the top right corner, just below the window minimize, maximize and close buttons, you will see 4 options, select the Open Settings (JSON) option.
  4. Once the JSON file is open, search for code-runner.executorMap and hit enter.
  5. Now many things will be visible on the screen for you, but you must look for "csharp": "scriptcs", replace with this "csharp": "cd $dir && dotnet run $fileName",
  6. Save everything and you are good to goooo :)
0
On

I did that, but I just get the error "Chocolately-WriteError" kind like that, even in CMD / PowerShell (ofc with admin). So I print the code, modify the script to this:

try {
    $tools = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"

    if (Test-Path "$tools\..\lib") {
        Remove-Item "$tools\..\lib" -Recurse -Force
    }

    # Handle upgrade from previous packages that installed to the %AppData%/scriptcs folders.
    $oldPaths = @(
        "$env:APPDATA\scriptcs",
        "$env:LOCALAPPDATA\scriptcs"
    )

    $oldPaths | foreach {
        # Remove the old user-specific scriptcs folders.
        if (Test-Path $_) {
            Remove-Item $_ -Recurse -Force
        }

        # Remove the user-specific path that got added in previous installs.
        # There's no Uninstall-ChocolateyPath yet so we need to do it manually.
        # https://github.com/chocolatey/chocolatey/issues/97
        $envPath = $env:PATH
        if ($envPath.ToLower().Contains($_.ToLower())) {
            $userPath = [Environment]::GetEnvironmentVariable("Path","User")
            if($userPath) {
                $actualPath = [System.Collections.ArrayList]($userPath).Split(";")
                $actualPath.Remove($_)
                $newPath =  $actualPath -Join ";"
                [Environment]::SetEnvironmentVariable("Path", $newPath, "User")
            }
        }

        Write-Host "'$_' has been removed." -ForegroundColor DarkYellow
    }
    Update-SessionEnvironment
    # End upgrade handling.

    echo 'installed scriptcs'
} catch {
    echo 'scriptcs' "$($_.Exception.Message)"
    throw
}

And it worked! So I think we will modify it to this.

0
On

If you are using macOS and Visual Studio code and want to run a single csharp file, you can follow these steps.

  1. Install mono using Homebrew brew install mono
  2. Open Visual Studio code and then open the command palette by pressing Cmd + shift + p
  3. In the search box, type Open User Settings
  4. Once in the json file, search code-runner.executorMap. Then go to languages, search for Csharp. And change line to this "csharp": "csc $fileName && mono ./$fileNameWithoutExt.exe",
  5. Save the file.

Now you can run single csharp file.

0
On

Your editor is using "scriptcs" to run the c# code by default. You can either change the default code runner to "dotnet run" or just install the scriptcs program. if you don't want to do the latter. follow these steps to change the default C# code runner for VS code

  1. press ctrl + shift + p to open the command pallet
  2. Type "settings.json" and click on the one that says open user settings.
  3. once in the json file type "code-runner.executorMap" it will automatically write the default code runners for various languages, search for Csharp and change "scriptcs" to "dotnet run" and save.
  4. Profit?