.Net Framework的Visual Studio代码

Aci*_*der 36 .net visual-studio-code

我很难弄清楚是否以及如何使用Visual Studio Code来开发和调试无法在.Net Core上运行的C#.Net程序的命令行/控制台/库,即它们需要.Net Framework.我需要访问没有.Net Core提供程序的Oracle,但它确实有一个Managed .Net Framework提供程序.我使用VS 2015/2017完成此任务,但如果我可以编写,构建和调试.Net Framework目标C#程序,我想切换到VS Code.我试过谷歌搜索,找不到任何东西.

小智 39

好吧,虽然我迟到了,但我发布了这个答案,因为它可能会帮助其他人面对这个问题,这对我来说非常累人.

首先,Visual Studio Code的最新更新确实支持.NET Framework的构建和调试项目,但它非常有限:

C#扩展支持有限的完整.NET框架调试.它只能使用便携式PDB调试64位应用程序.https://github.com/OmniSharp/omnisharp-vscode/wiki/Desktop-.NET-Framework

正如OmniSharp(负责C#扩展)github所说,该项目必须是带有便携式PDB的64位应用程序.

但是,即使在阅读了关于这个主题的许多问题和讨论之后,对我来说仍然有点不清楚是什么必要的步骤.因此,我将在这里向我们展示一个小指南,其中包含我所遵循的步骤,这对我有用,并且希望对您也有用.

1)必要的文件/文件夹是:

a).vscode与launch.json和tasks.json

b)exe应用程序的bin\Debug文件夹以及可能要创建引用的程序集

d).csproj和Program.cs文件

e)可选的批处理文件,其目的我将在后面描述

2)安装MSBuild 15

3)在.csproj文件中:

  • 将Project Sdk ="Microsoft.NET.Sdk"更改为Project ToolsVersion ="15.0"xmlns ="http://schemas.microsoft.com/developer/msbuild/2003"

  • 在第一个属性组中,我们必须将OutputType设置为Exe(默认值可能是dll),删除TargetFramework属性,替换为值为v4.6.1的TargetFrameworkVersion(例如.NET Framwork 4.6.1,例如可能为4.7) ,最后把运行时win-x64和win7-x64(以及编译器可能会抱怨的任何其他).第一组应如下所示:

<PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
   <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

  • 使用以下itens设置另一个属性组:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <PlatformTarget>x64</PlatformTarget>
  <DebugSymbols>true</DebugSymbols>
  <DebugType>portable</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

一些注释:使用的条件表明这些属性仅在传递给编译器的配置为Debug且平台为"AnyCPU"时才适用,您可能希望插入具有不同值的其他条件,甚至根本不使用条件; 这里最重要的值是:PlatformTarget属性必须x64,DebugType必须是可移植的 ; 输出路径设置为bin\Debug.

  • 由于我们没有使用Microsoft Sdk,我们必须包含Program.cs,以便编译器可以找到它:

<ItemGroup>
  <Compile Include="Program.cs" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

  • 为您的项目创建必要的参考; 例:

<ItemGroup>
  <Reference Include="mscorlib" />
  <Reference Include="System.Core" />
  <Reference Include="System.Windows" />
  <Reference Include="System.ServiceModel" />  
  <Reference Include="System.Net" />
  <Reference Include="System.Xml" />
  <Reference Include="System" />
  <Reference Include="System.Xml.Linq" />
  <Reference Include="System.Data.DataSetExtensions" />
  <Reference Include="Microsoft.CSharp" />
  <Reference Include="System.Data" />
  <Reference Include="System.Net.Http" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

  • 最后导入以下工具(确保按照此处公开的顺序,将其置于开头,例如生成错误)

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>x64</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>portable</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>x64</PlatformTarget>
    <DebugType>portable</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.cs" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="mscorlib" />
    <Reference Include="System.Core" />
    <Reference Include="System.Windows" />
    <Reference Include="System.ServiceModel" />  
    <Reference Include="System.Net" />
    <Reference Include="System.Xml" />
    <Reference Include="System" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
  </ItemGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

</Project>
Run Code Online (Sandbox Code Playgroud)

整个事情应该是这样的:

{
   "version": "0.2.0",
   "configurations": [
        {
            "name": "MyLauncher",
            "type":"clr",
            "request": "launch",
            "preLaunchTask": "mybuild",
            "program": "${workspaceFolder}/bin/Debug/<project>.exe",
            "args":[],
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
        { other configurations...
        }
    ,]
}
Run Code Online (Sandbox Code Playgroud)

4)在launch.json中:

  • 创建一个新配置(例如MyLauncher),其类型必须是clr,并指向您的程序; preLaunchTask将设置为手动配置的 - 例如"mybuild" - 将在tasks.json中指定; 配置的一个例子是:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "mybuild",
            "command":"<path to msbuild>\MSBuild.exe",
            "type":"shell",
            "args":[
                "<project>.csproj",
                "/t:Build",
                "/p:Configuration=Debug",
                "/p:Platform=\"AnyCPU\""
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

5)在tasks.json中:

  • 使用构建项目的命令创建任务"mybuild"
  • 我们将在这里使用MSBuild 15(不要使用dotnet构建 - 至少它对我不起作用)
  • 您可以使用构建项目的参数直接指向(路径)\ MSBuild.exe(或msbuild.exe,如果它在%PATH%中)文件.下面显示了一个示例,请注意我已将配置设置为调试,平台设置为AnyCPU,与.csproj文件中设置的条件匹配,同时请注意"AnyCPU"中的反斜杠是因为使用了引号分数.

"(path)\MSBuild.exe" (project).csproj /t:Build /p:Configuration=Debug /p:Platform="AnyCPU"
Run Code Online (Sandbox Code Playgroud)

  • 但还有另一种方法,使用.bat文件; 在我的情况下,MSBuild.exe的路径有空格,并且在任务运行时产生错误,因此我将以下代码放在.bat文件中(将记事本另存为name.bat):

    "(path)\ MSBuild.exe"(项目).csproj/t:Build/p:Configuration = Debug/p:Platform ="AnyCPU"

然后将"mybuild"任务设置为:

{
    "label": "mybuild",
    "command":"build.bat",
    "type":"shell",
    "args":[]
}
Run Code Online (Sandbox Code Playgroud)

其中build.bat是我用前面的代码创建的批处理文件

6)在此之后,您可能必须保存,关闭并重新打开文件(这很多次修复了我的问题)

7)将调试器中的配置设置为MyLauncher:

打印

8)使用绿色播放按钮运行代码; 它将调用MyLaunch,首先使用MSBuild 15构建您的项目,然后运行exe文件

就是这样.

希望它能帮到你.

如果您能提供任何反馈,那将是很棒的.

问候

<PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
   <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

  • 感谢冗长的回答,但是我将回到Visual Studio:'(我花费的时间少于此:'( (4认同)
  • @Abr 对于“只是工作”,你有 Visual Studio,而不是 vs Code (2认同)

feO*_*O2x 11

我刚刚创建了一个简单的控制台应用程序,并自定义了csproj文件。之后,我可以将OmniSharp调试器附加到完整的.NET Framework应用程序。csproj文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net47</TargetFramework>
    <PlatformTarget>x64</PlatformTarget>
    <DebugType>portable</DebugType>
  </PropertyGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

我只是遵循官方文档:我改为TargetFramework在.NET 4.7、64 PlatformTarget位和DebugType便携式上运行。

此外,我更新了launch.json:

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            "name": ".NET Launch (console)",
            "type": "clr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net47/FullNetInVsCode.exe",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "name": ".NET Attach",
            "type": "clr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ,]
}
Run Code Online (Sandbox Code Playgroud)

在这个文件中,我只是在两个JSON对象中都更改typeclr,并program以exe文件为目标。

之后,我可以设置一个断点,只需按F5键即可在完整的.NET框架上开始调试:

在VS Code中调试完整的.NET框架


Aci*_*der 5

https://code.visualstudio.com/docs/languages/csharp

引用:

注意:VS Code 不支持调试在桌面 .NET Framework上运行的应用程序。

看起来 Visual Studio 'full-fat' IDE 仍然是 .Net Framework 的要求。大可惜。


Mat*_*ram 0

不幸的是,它没有 C/C++ 的智能感知功能,只有语法突出显示:code.visualstudio.com/docs/languages 编辑:也没有 C/C++ 的调试器集成。git 集成确实很棒!似乎更适合 Web 应用程序,调试器适用于 Node.js

虽然这没有指定 C#,但理所当然的是,应用了相同的标准(即没有调试器,也没有编译功能)。

引用摘自对《Visual Studio Code 到底是什么?》第一个答案的评论。