Visual Studio 17.14.6环境变量丢失致测试运行器失败,如何解决?
问题描述
使用Visual Studio 17.14.6(2025年6月版本)时,环境变量意外被清除:
env:PATH丢失了c:\program files\dotnet和c:\windows\system32路径env:DOTNET_ROOT环境变量丢失
这导致Visual Studio Test Runner运行失败,报错信息如下:
8.0.14 .NET location: Not found Learn more: https://aka.ms/dotnet/app-launch-failed Download the .NET runtime: https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=win-x64&os=win10&apphost_version=8.0.14 Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process for source(s) '...' exited with error: You must install .NET to run this application. App: ...\bin\Debug\net9.0\testhost.exe ... ========== Test discovery aborted: 0 Tests found in 50.3 ms ==========
解决步骤
1. 恢复环境变量
临时恢复(仅当前会话有效)
打开管理员权限的PowerShell/命令提示符,执行以下命令:
- 补充
PATH路径:$env:PATH = "c:\windows\system32;c:\program files\dotnet;" + $env:PATH - 设置
DOTNET_ROOT:$env:DOTNET_ROOT = "c:\program files\dotnet"
执行后重启Visual Studio,测试Test Runner是否正常工作。
永久恢复(系统级生效)
- 右键此电脑 → 属性 → 高级系统设置 → 环境变量
- 在「系统变量」区域:
- 找到
PATH变量,点击「编辑」,添加两条路径(建议放在列表顶部):c:\program files\dotnet c:\windows\system32 - 点击「新建」,变量名填
DOTNET_ROOT,变量值填c:\program files\dotnet
- 找到
- 依次点击「确定」保存设置,重启电脑后生效。
2. 修复VS与.NET安装
如果环境变量恢复后问题仍存在:
- 打开Visual Studio Installer,选中你的VS实例,点击「修改」→「修复」,完成后重启VS。
- 重新安装项目使用的.NET SDK(比如你用到的.NET 9.0),安装程序会自动修复环境变量关联。
3. 验证修复结果
打开PowerShell执行以下命令,确认环境变量和.NET状态:
echo $env:PATH # 检查是否包含c:\program files\dotnet和c:\windows\system32 echo $env:DOTNET_ROOT # 检查是否输出c:\program files\dotnet dotnet --version # 检查.NET是否能正常识别
如果输出符合预期,回到Visual Studio重新运行测试即可。
内容的提问来源于stack exchange,提问作者Eric Patrick
相关产品推荐
相关产品推荐

