.Net 框架的 Visual Studio 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47707095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio Code for .Net Framework
提问by Acid Rider
I am having difficulty figuring out if and how I can use Visual Studio Codeto develop and debug command-line/console/libraries of C#.Net programs which can not run on .Net Core, i.e. they require .Net Framework. I need to access Oracle which do not have a .Net Core provider but it does have a Managed .Net Framework provider. I use VS 2015/2017 for this task but would like to switch to VS Code if I could code, build and debug .Net Framework target C# programs. I have tried Google search and could not find anything.
我很难弄清楚是否以及如何使用 Visual Studio Code来开发和调试无法在 .Net Core 上运行的 C#.Net 程序的命令行/控制台/库,即它们需要 .Net Framework。我需要访问没有 .Net Core 提供程序但它有托管 .Net Framework 提供程序的 Oracle。我将 VS 2015/2017 用于此任务,但如果我可以编码、构建和调试 .Net Framework 目标 C# 程序,我想切换到 VS Code。我试过谷歌搜索,找不到任何东西。
回答by Mario Figueiredo
First thing, the more recent updates for Visual Studio Code do support building and debugging projects for the .NET Framework, but it is very limited.
首先,Visual Studio Code 的最新更新确实支持为 .NET Framework 构建和调试项目,但它非常有限。
The GitHub page for OmniSharp(responsible for the C# extension) says that:
OmniSharp的GitHub 页面(负责 C# 扩展)说:
The C# extension supports limited full .NET framework debugging. It can only debug 64-bit applications with portable PDBs.
C# 扩展支持有限的完整 .NET 框架调试。它只能调试带有可移植 PDB 的64 位应用程序。
But, even after reading many issues and discussions about this topic, it remained a little bit unclear for me what the necessary steps were, so I will expose here a little guide with the steps that I have followed and that worked for me, and hopefully, will also work for you.
但是,即使在阅读了有关该主题的许多问题和讨论之后,我仍然有点不清楚必要的步骤是什么,因此我将在此处提供一些指南,其中包含我遵循并对我有用的步骤,希望,也会为你工作。
The necessary files/folders are:
a.
.vscodewithlaunch.jsonandtasks.json.b.
bin\Debugfolder for your .exe application and the assemblies you might want to create a reference to.d. the
<project>.csprojandProgram.csfiles.e. optionally a batch file, whose purpose I will describe later.
Install MSBuild 15 (2017).
In the
<project>.csprojfile:change the
Project Sdk="Microsoft.NET.Sdk"toProject ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003".in the first
PropertyGroupwe must set theOutputTypetoExe(the default may bedll), remove theTargetFrameworkproperty, replacing withTargetFrameworkVersionwith valuev4.6.1(example for .NET Framwork 4.6.1, it may be 4.7 for instance), and finally put the runtimes win-x64 and win7-x64 (and any other that the compiler may complain). This firstPropertyGroupshould look like this:<PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers> </PropertyGroup>set another PropertyGroup` with the following items:
<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>
Some comments: the condition used signals that these properties only apply when the configuration passed to the compiler is Debug and the Platform is "AnyCPU", you might want to insert other conditions with different values, or even don't use a condition at all; the most import values here are: The PlatformTargetproperty mustbe x64and the DebugTypemust be portable; the output path is set to bin\Debug.
As we are not using the Microsoft SDK we must include the
Program.cs, so that the compiler can find it:<ItemGroup> <Compile Include="Program.cs" /> </ItemGroup>create the necessary references to your project, for example:
<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>finally import the following tools (make sure you follow the order exposed here, placing this in the beginning for instance you generate an error)
The whole thing should look like this:
<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>In the
launch.json:Create a new configuration(e.g.
MyLauncher), whose type must be clr, and that points to your program; thepreLaunchTaskwill be set to a manually configured one ("mybuild", for instance) that will be specified in thetasks.json; an example of the configuration is:{ "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... } ,] }
In the
tasks.json:Create a task
"mybuild"with the commands to build your project.We will use the MSBuild 15 here (don't use the dotnet build - at least it has not worked for me).
You can directly point to the
(path)\MSBuild.exe(ormsbuild.exe, if it is in the%PATH%) file with the arguments to build the project. One example is shown below, note that I've set the Configuration to Debug and the platform to AnyCPU, matching the condition Ive set in the.csprojfile, also note that the backslashes in\"AnyCPU\"are because of the use of the quotation marks.{ "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\"" ] } ] }but there is another way, using the
.batfile; in my case the path to theMSBuild.exehad spaces and that was generating an error when the task run, so that I've put the following code in a.batfile (save notepad asname.bat):"(path)\MSBuild.exe" (project).csproj /t:Build /p:Configuration=Debug /p:Platform="AnyCPU"and then set the
"mybuild"task to:{ "label": "mybuild", "command":"build.bat", "type":"shell", "args":[] }Where
build.batis the batch file I have created with the previous code.
After this, you might have to save, close and reopen the files (this many times fixes problems for me).
Set your configuration in the debugger to
MyLauncher:Run your code with the green play button; it will call the
MyLauncher, that first will build your project with MSBuild 15 and then run the exe file
必要的文件/文件夹是:
一种。
.vscode同launch.json和tasks.json。湾
bin\Debug.exe 应用程序和您可能想要创建引用的程序集的文件夹。d. 在
<project>.csproj和Program.cs文件。e. 可选的批处理文件,我将在后面描述其用途。
在
<project>.csproj文件中:将 更改
Project Sdk="Microsoft.NET.Sdk"为Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"。首先
PropertyGroup我们必须设置OutputType为Exe(默认可能是dll),删除TargetFramework属性,用TargetFrameworkVersion值替换v4.6.1(例如.NET Framwork 4.6.1,例如可能是4.7),最后把运行时win-x64和win7-x64(以及编译器可能会抱怨的任何其他内容)。这首先PropertyGroup应该是这样的:<PropertyGroup> <OutputType>Exe</OutputType> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <RuntimeIdentifiers>win-x64;win7-x64</RuntimeIdentifiers> </PropertyGroup>使用以下项目设置另一个 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>
一些评论:使用的条件表明这些属性仅在传递给编译器的配置为 Debug 且平台为“AnyCPU”时才适用,您可能想要插入具有不同值的其他条件,甚至根本不使用条件; 这里最重要的值是:PlatformTarget属性必须是x64并且DebugType必须是可移植的;输出路径设置为 bin\Debug。
由于我们没有使用 Microsoft SDK
Program.cs,因此必须包含,以便编译器可以找到它:<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>最后导入以下工具(确保您遵循此处公开的顺序,将其放在开头,例如您会生成错误)
整个事情应该是这样的:
<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>在
launch.json:创建一个新的配置(例如
MyLauncher),其类型必须是 clr,并且指向您的程序;的preLaunchTask将被设置为手动配置的一个("mybuild"例如)将在指定tasks.json; 配置的一个例子是:{ "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... } ,] }
在
tasks.json:"mybuild"使用命令创建任务以构建您的项目。我们将在这里使用 MSBuild 15(不要使用 dotnet 构建 - 至少它对我不起作用)。
您可以使用参数直接指向
(path)\MSBuild.exe(或msbuild.exe,如果在 中%PATH%)文件以构建项目。一个例子如下所示,注意我把Configuration设置为Debug,平台设置为AnyCPU,符合我在.csproj文件中设置的条件,还要注意里面的反斜杠\"AnyCPU\"是因为使用了引号。{ "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\"" ] } ] }但还有另一种方法,使用
.bat文件;在我的情况下,MSBuild.exe有空格的路径在任务运行时会产生错误,因此我将以下代码放入.bat文件中(将记事本另存为name.bat):"(path)\MSBuild.exe" (project).csproj /t:Build /p:Configuration=Debug /p:Platform="AnyCPU"然后将
"mybuild"任务设置为:{ "label": "mybuild", "command":"build.bat", "type":"shell", "args":[] }build.bat我用前面的代码创建的批处理文件在哪里。
在此之后,您可能需要保存、关闭并重新打开文件(这多次为我解决了问题)。
将调试器中的配置设置为
MyLauncher:使用绿色播放按钮运行您的代码;它将调用
MyLauncher,首先将使用 MSBuild 15 构建您的项目,然后运行 exe 文件
So that was it.
就是这样。
Here are some references:
以下是一些参考:
回答by feO2x
I just created a simple console application and customized the csproj file. Afterwards, I could attach the OmniSharp debugger to a full .NET framework application. The csproj file looks like this:
我刚刚创建了一个简单的控制台应用程序并自定义了 csproj 文件。之后,我可以将 OmniSharp 调试器附加到完整的 .NET 框架应用程序。csproj 文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net47</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<DebugType>portable</DebugType>
</PropertyGroup>
</Project>
I just followed the official documentation: I changed TargetFrameworkto run on .NET 4.7, the PlatformTargetto 64 bit and the DebugTypeto portable.
我只是按照官方文档进行操作:我更改TargetFramework为在 .NET 4.7 上运行,PlatformTarget64 位和DebugType可移植的。
Furthermore, I updated launch.json:
此外,我更新了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}"
}
,]
}
In this file, I just changed typeto clrin both JSON objects, and targeted programto the exe file.
在这个文件中,我只是在两个 JSON 对象中都更改type为clr,并针对programexe 文件。
Afterwards, I could set a break point and simply press F5 to start debugging on full .NET framework:
之后,我可以设置一个断点,然后只需按 F5 即可在完整的 .NET 框架上开始调试:
回答by Acid Rider
https://code.visualstudio.com/docs/languages/csharp
https://code.visualstudio.com/docs/languages/csharp
Quote:
引用:
Note: VS Code does not support debuggingapplications running on the Desktop .NET Framework.
注意:VS Code 不支持调试在桌面 .NET Framework上运行的应用程序。
It looks like Visual Studio 'full-fat' IDE remains a requirement for .Net Framework. Major pity.
看起来 Visual Studio 'full-fat' IDE 仍然是 .Net Framework 的要求。大可惜。
回答by Matthew Pigram
Unfortunately, it doesn't feature intellisense for C/C++, only syntax highlighting: code.visualstudio.com/docs/languages EDIT: no debugger integration for C/C++ either. The git integration is really nice though! Seems more designed for web applications, the debugger works for node.js
不幸的是,它没有 C/C++ 的智能感知功能,只有语法突出显示:code.visualstudio.com/docs/languages 编辑:C/C++ 也没有调试器集成。不过 git 集成真的很好!似乎更适合 Web 应用程序,调试器适用于 node.js
Whilst this does not specify C#, it stands to reason that the same standards apply (which is that there is no debugger and no compile functionality).
虽然这没有指定 C#,但有理由应用相同的标准(即没有调试器和编译功能)。
Quote taken from comment on first answer of What exactly is Visual Studio Code?
引自对什么是 Visual Studio Code 的第一个答案的评论?


