15

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.

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39
  • There are several different issues here: 1) your code is wrong (but that's not the immediate problem), 2) it's possible that scriptcs isn't installed correct, and/or 3) You might have embedded spaces in your path names. STRONG SUGGESTION: If you want to code in C#, then Visual Studio is probably your best bet for any "serious" work: https://visualstudio.microsoft.com/downloads/ – FoggyDay Dec 10 '19 at 07:13
  • @FoggyDay, correct the code. 2) Re-installed the ScriptCs from VS Code extension still the same,and 3) There are no spaces in my path names of file - still getting the same error. – Rohan Rao Dec 10 '19 at 07:53
  • @FoggyDay yes that's what I was thinking. I'd better stay with what I had - Visual Studio.Thanks :) – Rohan Rao Dec 10 '19 at 09:01

10 Answers10

18
  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

Mihaeru
  • 533
  • 2
  • 6
  • I already have c# extension installed, but let me check for rest of the steps you mentioned. Thanks :) – Rohan Rao Dec 10 '19 at 07:59
  • 1
    I followed your steps, but still getting the same error. I re-installed the scriptcs Runner, I double checked my code and checked my path so it doesn't have any spaces. – Rohan Rao Dec 10 '19 at 08:12
  • You even get the same error when you created the new Console application project (in Command or PowerShell console) and start it with `dotnetrun --project YourProjectName` in the VS Code Terminal Console? I just tried it and it worked well. I assume that you have the .NET SDK installed. No idea what else could be missing. – Mihaeru Dec 10 '19 at 08:37
  • `dotnet run --project my-project.csproj` worked for me. Without ".csproj" it didn't. – s3c Nov 02 '22 at 10:30
6

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”
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
3

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

Cryolithic
  • 151
  • 2
  • 4
1

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.

Mathew
  • 11
  • 1
  • 2
    you should answer the question fully here and not simply link to an answer in another location, though references to primary source material are encouraged. – Peter Oehlert Mar 17 '22 at 14:46
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31295329) – MD. RAKIB HASAN Mar 18 '22 at 11:09
0

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

dotnet run

ABlue
  • 664
  • 6
  • 20
0
  1. File>Prerferences>Settings
  2. Here on the top right corner , just below the window minimize, maximize and close buttons, you will see 4 options, select the open json settings options.
  3. Once the json file is open, type code-runner.executorMap and hit enter.
  4. 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",
  5. Save everything and you are good to goooo :)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 07 '23 at 23:46
0

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?
Ishmeet
  • 1
  • 1
0

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.

Kin
  • 11
  • 1
-1

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);
    }
}
Igor Zhurba
  • 157
  • 1
  • 7
-1

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

Ho30
  • 1
  • 4
  • Welcome to Stack Overflow! When you can, place your code, errors, etc in the post itself using the [code format](https://stackoverflow.com/editing-help#code) instead of uploading images. This helps make your post more readable, and prevents issues if the images are ever deleted. [From Review](https://stackoverflow.com/review/late-answers/28453160) – Adam Marshall Mar 04 '21 at 01:00