如何用Visual Studio Code创建C#项目?解决dotnet指令无响应问题
Hey there! Let's walk through both how to create a C# project in VS Code and fix that frustrating issue where dotnet new console does nothing at all.
First, let’s cover the standard workflow (once your tools are working properly):
- Make sure you have Visual Studio Code installed, plus the C# for Visual Studio Code (OmniSharp) extension from the marketplace.
- Install a compatible .NET SDK (we’ll talk about version compatibility in the fix section below).
- Create a new folder for your project, then open it in VS Code (File > Open Folder).
- Open the integrated terminal (press `Ctrl + `` or go to View > Terminal).
- Run
dotnet new console– this generates the basic "Hello World" project structure (Program.cs, .csproj file, etc.). - To test it, run
dotnet run– you should see "Hello World!" printed in the terminal.
dotnet new console Command If executing dotnet new console gives no output and no project files are created, here are the most common fixes to try:
1. Verify the .NET SDK is installed and in your PATH
First, check if your system recognizes the dotnet command at all. In the terminal, run:
dotnet --version
- If this command also does nothing or throws a "not found" error, your SDK wasn’t added to your system’s PATH during installation.
- On Windows: Go to System Properties > Advanced > Environment Variables, find the
Pathvariable under System Variables, and add the path to your .NET SDK’sbinfolder (usuallyC:\Program Files\dotnet\). Restart VS Code (and your terminal) after making changes. - On macOS/Linux: Check your shell config file (like
~/.bashrcor~/.zshrc) to ensure the dotnet path is added. You can manually add it withexport PATH="$PATH:/usr/local/share/dotnet"then restart your terminal/VS Code.
- On Windows: Go to System Properties > Advanced > Environment Variables, find the
- If
dotnet --versionreturns your 2.1.10 version, move to the next step.
2. Check Folder Permissions
Sometimes the issue is just that your user account doesn’t have write access to the folder you’re working in:
- On Windows: Try opening VS Code as an administrator (right-click VS Code > Run as administrator) then re-run the command.
- On macOS/Linux: Run
ls -ld /path/to/your/folderto check permissions. If needed, you can adjust permissions withchmod -R 755 /path/to/your/folder(or usesudo dotnet new consoletemporarily, though it’s better to fix folder permissions long-term).
3. Clear .NET Cache
Corrupted cache files can cause weird behavior. Run these commands in the terminal to clear them:
dotnet nuget locals all --clear
Wait for the process to finish, then try dotnet new console again.
4. Upgrade Your .NET SDK Version
.NET 2.1.10 is a very old release (it’s out of support now) and might have compatibility issues with newer versions of VS Code or the C# extension. Consider upgrading to a supported LTS (Long-Term Support) version like .NET 6 or .NET 8:
- Uninstall the old 2.1.10 SDK from your system’s Apps & Features (Windows) or using your package manager (macOS/Linux).
- Grab a supported LTS .NET SDK version, install it, and make sure the installation adds it to your PATH.
- After installing, run
dotnet --versionto confirm the new version is recognized, then try creating the project again.
5. Test with the Full Path to dotnet
If all else fails, try running the command using the full path to the dotnet executable to rule out PATH issues:
- On Windows:
"C:\Program Files\dotnet\dotnet.exe" new console - On macOS/Linux:
/usr/local/share/dotnet/dotnet new console
If this works, you know the problem is definitely with your PATH configuration.
Once you get the dotnet command responding, the rest of the project setup should be straightforward. Let me know if any of these steps get you up and running!
内容的提问来源于stack exchange,提问作者Alberto

