如何配置 Visual Studio Code 以在 PATH 中没有 .NET SDK 的情况下使用 Visual Studio Professional 2019 工具?

问题描述 投票:0回答:1

我安装了 Visual Studio Professional 2019:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.com
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\amd64\MSBuild.exe
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\csc.exe

当我使用

code .
从命令行打开项目文件夹时,我在 Visual Studio Code 中得到以下输出:

Starting Spawn .NET server...
Starting opening a solution...
Starting processing the solution file "path\to\file.sln" in Dev Kit server...
Failed to find dotnet from path with "where.exe dotnet".
Cannot find .NET SDK installation from PATH environment. C# DevKit extension would not work without a proper installation of the .NET SDK accessible through PATH environment. Rebooting might be necessary in some cases. Check the PATH environment logged in the C# DevKit logging window. In some cases, it could be affected how VS code was started.
PATH=C:\Windows\System32\;C:\Users\%USERNAME%\AppData\Local\Programs\Microsoft VS Code\bin\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\amd64\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\;C:\ProgramData\chocolatey\bin\;C:\Program Files\Git\cmd\;C:\Program Files\TortoiseSVN\bin\;C:\Windows\System32\WindowsPowerShell\v1.0\;
Using local .NET runtime at "c:\Users\%USERNAME%\AppData\Roaming\Code\User\globalStorage\ms-dotnettools.vscode-dotnet-runtime\.dotnet\8.0.8~x64~aspnetcore\dotnet.exe"
.NET server started and IPC established in 1003ms
Completed Spawn .NET server (1858ms)
Completed processing the solution file "path\to\file.sln" in Dev Kit server (2442ms)
Starting restoring NuGet packages...
Selected configuration: <Default>, active configuration: Debug|Any CPU
Completed opening a solution (2471ms)
Starting command: "dotnet.exe" restore path\to\file.sln --interactive...
Failed to monitor project loading status.
Starting Query for all projects...
Failed at 'Query for all projects' (38ms) with error: Error: Exception while talking to proxy: Error: One or more errors occurred. (No installed .NET SDK was found on the computer. If it is installed, make sure it can be found through the PATH environment variable.)

我想知道如何诊断和解决问题,最好使用 JSON 文件,主要是

.vscode/settings.json
文件。我不想使用
dotnet.exe
。我想我需要找到 Visual Studio Professional 2019 附带的“.NET SDK”。

c# visual-studio-code msbuild visual-studio-2019 .net-sdk
1个回答
0
投票

对于Build,看起来很简单。无需编辑项目文件,只需首先添加 MSBuild.exe 和 .NET Framework 的路径(系统环境变量)即可。

当您安装了 VS Professional 2019 时,MSBuild.exe 的目录应该是

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin
。对于.NET Framework,首先检查这个目录=>
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework
,如果您的项目所针对的.NET Framework不存在,请尝试从这里单独下载并安装。

添加路径后,打开/重新启动 VS Code > 文件 > 打开文件夹并打开项目文件夹。按

Ctrl + ~
在 VS Code 中打开终端窗口,运行
msbuild
,项目应该已构建。

enter image description here

enter image description here

对于运行调试,需要launch.json和tasks.json文件。我测试了Mario Figueiredo分享的方法这里,我可以确认它工作正常。

enter image description here

我知道您不想使用 C# 开发工具包扩展,但我不确定您是否考虑在 VS Code 中使用 C# 扩展。由于 C# 扩展提供了一些用于在 VS Code 中开发和调试 C# 的功能,因此它包括 OmniSharp,这使得在 VS Code 中运行和调试 .NET Framework 项目成为可能,尽管有限。

我想,也许你想调用使用 VS 调试器(在 VS Code 中)而不是 VS Code 或扩展相关的调试器,对吗?但我认为这是不可能的,或者可能很复杂或很难使其发挥作用。我只是好奇,如果是这样,为什么不直接使用 VS 2019 来调试:) 不管怎样,我认为使用 C# 扩展和 OmniSharp 来运行和调试项目是必要的(我没有找到直接调用/使用 MSBuild 的方法)在不使用 C# 扩展和 OmniSharp 的情况下调试项目)。

如果您考虑仅使用 C# 扩展在 VS Code 中运行和调试 .NET Framework 项目,我想我可以分享一些注释/技巧。

  1. Mario Figueiredo 的解决方案包括构建和调试。

  2. 由于限制,需要编辑工程文件,但不需要修改很多属性。您可以关注以下属性:

    (一)

    <OutputType>Exe</OutputType>

    (二)

    <PlatformTarget>x64</PlatformTarget>

    (三)

    <DebugType>portable</DebugType>

  3. 关于launch.json文件,重点关注以下内容:

    (一)

    "type":"clr"

  4. 关于tasks.json文件,重点关注以下内容:

    (一)

    "command":"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\MSBuild\\Current\\Bin\\MSBuild.exe"

    (二)

    "type":"shell"

  5. 您可能需要取消选中

    Ominisharp: Use Modern Net
    选项。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.