Visual Studio Code,Windows上的C#支持

23
我希望尝试一下微软的新编辑器 - Visual Studio Code。我想实现一个简单的应用程序(如Hello,World),并能够进行调试。但是在大量搜索和尝试后,我没有成功。这有可能吗?我安装了Windows 10和Visual Studio 2015。
我尝试做的事情:
1)取消注释tasks.json文件中的以下内容:
{
"version": "0.1.0",
"command": "msbuild",
"args": [
    // Ask msbuild to generate full paths for file names.
    "/property:GenerateFullPaths=true"
],
"taskSelector": "/t:",
"showOutput": "silent",
"tasks": [
    {
        "taskName": "build",
        // Show the output window only if unrecognized errors occur.
        "showOutput": "silent",
        // Use the standard MS compiler pattern to detect errors, warnings
        // and infos in the output.
        "problemMatcher": "$msCompile"
    }
]

但是编译(ctrl-shift-b)失败,报错信息为:“无法启动外部程序msbuild。spawn msbuild ENOENT”

2)阅读这篇文章:http://michaelcrump.net/creating-and-debugging-console-apps-with-vscode/,但我没有任何project.json文件,同时“dnx . run”也会报错“System.InvalidOperationException: 无法解析项目

是否有一种简单的方法在VS Code中运行简单的C#应用程序?我想知道为什么会这么复杂。可能是我漏掉了什么,欢迎提供任何想法。

更新1:将msbuild添加到路径中有所帮助。


你在这方面有进展吗?我也刚刚发现了VSC,对于小改动来说,它是一个很好的快速替代品,但我遇到了同样的错误。 - svanelten
@svanelten,很抱歉,不行。但是我使用的是旧版本的VS Code,你用的是哪个版本? - pfedotovsky
当前版本是0.5.0。我非常期待它成熟,可以取代Sublime Text成为我的首选。 - svanelten
1
@pfedotovsky 你好,现在它确实可以工作了 - 我将msbuild添加到我的用户PATH中,重新启动后它就可以工作了。之前我只将其添加到系统路径中。 - svanelten
@svanelten,将msbuild添加到PATH有所帮助,但我仍然在我的Xamarin.Forms项目中遇到问题。有很多与OmniSharp相关的错误。 - pfedotovsky
显示剩余3条评论
3个回答

7
在VS Code中,C#支持被优化为跨平台.NET开发(DNX)(有关另一相关文章,请参见使用ASP.NET 5和VS Code进行工作)。我们在VS Code中的重点是成为一个出色的编辑器,用于跨平台C#开发 - 例如,许多Unity开发人员一直在享受使用VS Code代替Mono Develop的乐趣。我们支持通过Mono跨平台调试C#应用程序(请参见Mono调试)。由于这个重点,许多标准的C#项目类型在VS Code中无法被识别。一个不支持的项目类型的例子是ASP .NET MVC应用程序。在这些情况下,如果你只是想要一个轻量级的工具来编辑文件 - VS Code可以满足你的需求。如果你想要这些项目的最佳体验,并且在Windows上进行开发,我们建议你使用Visual Studio Community。

来自:code.visualstudio.com/Docs/languages/csharp


更新,2017年6月:

C#现在可以使用微软的VS Code C#扩展程序在Visual Studio Code中进行支持:https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp

您可以通过在“扩展”中搜索“C#”来安装它。或者,打开一个.cs文件时,会提示您安装该扩展程序。

该扩展程序支持在.NET CoreMono上运行的C#应用程序的调试,但不支持桌面.NET Framework上运行的应用程序的调试 - 仍然建议使用Visual Studio Community以获得完整的.NET项目支持。

有关VS Code的C#支持的更多信息,请参见https://code.visualstudio.com/docs/languages/csharp

关于VS Code C#扩展的市场页面,请参见https://marketplace.visualstudio.com/items?itemName=ms-vscode.csharp


6

您可以设置msbuild来构建完整的.NET应用程序,注意您必须在路径中拥有msbuild或在command属性中提供完整路径。以下是绑定到ctrl + shift + b的示例构建任务:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "MSBuild.exe",
    "args": [
        // $msCompile requires GenerateFullPaths=true, rest is optional. I set it to build Debug/Any CPU and use parallel project build
        "${workspaceRoot}/src/ExampleSolution.sln",
        "/p:Configuration=Debug;Platform=Any CPU;GenerateFullPaths=true",
        "/m"
    ],
    "taskSelector": "/t:",
    "showOutput": "silent",
    "echoCommand": true,
    "tasks": [
        {
            "taskName": "Rebuild",
            // Required if you want "Rebuild", not build as your target and still use ctrl + shift + b hotkey
            "isBuildCommand": true,
            // Show the output window only if unrecognized errors occur.
            "showOutput": "silent",
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

更新:如果您想设置构建和单元测试运行,请使用以下方式(使用nunit控制台运行程序和cmd作为任务,绕过vscode仅支持单一任务的限制):

{
    "version": "0.1.0",
    "command": "cmd",
    "args": [
        "/c"
    ],
    "showOutput": "always",
    "echoCommand": true,
    "tasks": [
        {
            "isBuildCommand": true,
            "taskName": "MSBuild.exe",
            "args": [
                "${workspaceRoot}/src/Example.sln",
                "/t:Rebuild",
                "/p:Configuration=Debug;Platform=Any CPU;GenerateFullPaths=true",
                "/m"
            ],
            "showOutput": "always",
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "nunit3-console.exe",
            "args": [
                "${workspaceRoot}/src/Example.sln"
            ],
            "showOutput": "always"
        }
    ]
}

2

我遇到了同样的问题。将直接路径更改为msbuild即可解决:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "windows": {
        "command": "C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe"
    },
   // "command": "msbuild",
    "args": [
        // Ask msbuild to generate full paths for file names.
        "/property:GenerateFullPaths=true"
    ],
    "taskSelector": "/t:",
    "showOutput": "silent",
    "tasks": [
        {
            "taskName": "build",
            // Show the output window only if unrecognized errors occur.
            "showOutput": "silent",
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接