VS Code集成终端启动失败:CreateProcess原生异常(ASP.NET MVC项目)
ASP.NET MVC项目VS Code集成终端启动失败排查与解决
问题描述
正在开发ASP.NET MVC Web应用,外部命令行执行以下命令可正常运行:
dotnet restore ProjectDirectory
接着执行:
dotnet run --project ProjectDirectory
外部命令行运行完全正常,但在VS Code集成终端中通过配置launch.json和tasks.json启动时出错。
配置文件如下:
launch.json
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. "version": "2.0.0", "configurations": [ { "name": ".NET Core Launch (web)", "type": "coreclr", "request": "launch", "preLaunchTask": "restore", "program": "C:/Windows/System32/dotnet.exe", // 路径分隔符用/而非\\ "args": ["run", "--project", "${workspaceFolder}"], "cwd": "${workspaceFolder}", "stopAtEntry": false, "serverReadyAction": { "action": "openExternally", "pattern": "\\\\bNow listening on:\\\\s+(https?://\\\\S+)" }, "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, "sourceFileMap": { "/Views": "${workspaceFolder}/Views" } }, { "name": ".NET Core Attach", "type": "coreclr", "request": "attach" } ] }
tasks.json
{ "version": "2.0.0", "tasks": [ { "label": "build", "command": "dotnet", "type": "process", "args": [ "build", "${workspaceFolder}/MyWebApp.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "publish", "command": "dotnet", "type": "process", "args": [ "publish", "${workspaceFolder}/MyWebApp.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, { "label": "watch", "command": "dotnet", "type": "process", "args": [ "watch", "run", "${workspaceFolder}/MyWebApp.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, // 预启动任务 { "label": "restore", "command": "dotnet", "type": "process", "args": [ "restore", "${workspaceFolder}" ], "problemMatcher": "$msCompile" } ] }
按F5启动时,集成终端执行dotnet restore ProjectDirectory时抛出错误:
The terminal process failed to launch: A native exception occurred during launch (CreateProcess failed)
已尝试:
- 查阅VS Code官方终端故障排查文档并尝试所有建议;
- 搜索Stack Overflow等平台未找到有效方案。
解决方案建议
1. 切换默认终端Shell
VS Code集成终端默认Shell可能存在兼容性问题,切换到Windows Command Prompt:
- 打开设置(
Ctrl+,); - 搜索
terminal.integrated.defaultProfile.windows,选择Command Prompt; - 重启VS Code后重试F5启动。
2. 修正launch.json配置
- 把
program字段改为"dotnet",依赖系统环境变量自动定位dotnet.exe,避免绝对路径解析错误; - 直接指定csproj文件路径而非工作区文件夹,减少自动查找项目文件的不确定性。
修改后相关配置:
"program": "dotnet", "args": ["run", "--project", "${workspaceFolder}/MyWebApp.csproj"],
3. 调整tasks.json的restore任务
将restore任务的参数改为直接指向csproj文件,避免多项目文件夹下的解析冲突:
{ "label": "restore", "command": "dotnet", "type": "process", "args": [ "restore", "${workspaceFolder}/MyWebApp.csproj" ], "problemMatcher": "$msCompile" }
4. 以管理员身份运行VS Code
权限不足可能导致CreateProcess失败,右键VS Code图标选择“以管理员身份运行”后重试启动。
5. 重置VS Code终端配置
通过Ctrl+Shift+P打开命令面板,输入Preferences: Reset Settings重置所有设置,之后重新配置launch和tasks文件,排除自定义配置干扰。
内容的提问来源于stack exchange,提问作者SNBS
相关产品推荐
相关产品推荐

