如何配置VSCode的CodeRunner解决‘scriptcs未被识别’报错
Got it, let's get that Code Runner working for your C# code instead of hitting that annoying "scriptcs not recognized" error. Here are a couple of practical solutions tailored to how you write C#:
Solution 1: Use dotnet run for full .NET projects (integrate your existing workflow)
If you're working with a proper .NET project (has a .csproj file), we can configure Code Runner to run the same dotnet run command you already use manually:
- Open VS Code settings with
Ctrl + ,(Windows/Linux) orCmd + ,(Mac) - Search for Code Runner: Executor Map, then click "Edit in settings.json"
- Find the
"csharp"entry in the JSON object, and replace its value with:"csharp": "cd $dir && dotnet run" - Save the settings file, restart VS Code, and try running your code again. Code Runner will now switch to your project directory and execute
dotnet runjust like you do in the terminal.
Solution 2: Use dotnet script for single C# files (no .csproj needed)
If you often write standalone .cs scripts without a full project, dotnet script is the modern, official alternative to scriptcs:
- First, install the
dotnet-scripttool globally via your terminal:dotnet tool install -g dotnet-script - Head back to Code Runner's Executor Map settings (same steps as Solution 1) and update the
"csharp"entry to:"csharp": "dotnet script $fullFileName" - Save, restart VS Code, and you can now run single
.csfiles directly via Code Runner without any project setup.
Optional (Not Recommended): Install scriptcs
If you really want to stick with scriptcs (though it's less maintained than .NET's official tools), you'll need to:
- Download the scriptcs installer for your OS from its official distribution page
- Add the directory containing the
scriptcsexecutable to your system'sPATHenvironment variable - Restart VS Code so it picks up the updated PATH entry
After any of these solutions, test running your C# code with Code Runner's play button—you should be good to go!
内容的提问来源于stack exchange,提问作者myfunnyfella

