C# on Visual Studio Code

[comment]: # C# on Visual Studio Code

installation

.NET Core SDK = Develop apps with .NET Core and the SDK+CLI (Software Development Kit/Command Line Interface) tools

  • Install Visual Studio Code

  • Install Visual Studio Code extensions for C# Development

    • C# (C# for Visual Studio Code (powered by OmniSharp))

Create a C# project

  • Create a folder for your .NET application, e.g. NetApp.
  • Open a command windows and go to the folder.
  • Run
dotnet new

Output:

E:WorkNetApp>dotnet new
Created new C# project in E:WorkNetApp.

The command will creat a project.json file and a Program.cs file.

Build the project

  • Open the folder via Visual Studio
    Visual Studio Code will prompt:
    • Required assets to build and debug are missing from your project, Add them?
      Click Yes
      The operation is same as
      • Create a .vscode folder
      • Add a launch.json file into the .vscode folder
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}\bin\Debug\netcoreapp1.0\NetApp.dll",
            "args": [],
            "cwd": "${workspaceRoot}",
            "externalConsole": false,
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command.pickProcess}"
        }
    ]
}
    • Add a tasks.json file into the .vscode folder
{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}\project.json"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}
    • There are unresolved dependencies from 'project.json', Please execute the restore command to continue.
      Click Restore
      The operation will create a project.lock.json, which is same as run
dotnet restore
  • Build
    Press Ctrl + Shift + B,
    Output:
Project NetApp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling NetApp for .NETCoreApp,Version=v1.0
Compilation succeeded.
    0 Warning(s)
    0 Error(s)
Time elapsed 00:00:03.4166048

Run the project

  • Edit .vscode/task.json
{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}\project.json"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "run",
            "args": [
                "${workspaceRoot}\project.json"
            ],
            "isBuildCommand": false,
            "problemMatcher": "$msCompile"
        }
    ]
}
  • Run the project
    • Way 1:
      Press Ctrl + P
      Input "task ", need a space
      Select run
    • Way 2:
      Press Ctrl + Shift + P
      Input > Tasks: Run Tasks, press enter
      Select run
      Output:
Project NetApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hello World!
  • Trick: Run the project with Ctrl+Shift+B
    Change the .vscode/task.json by
    set task build's isBuildCommand as false,
    set task run's isBuildCommand as true,
    like:
{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [
                "${workspaceRoot}\project.json"
            ],
            "isBuildCommand": false,
            "problemMatcher": "$msCompile"
        },
        {
            "taskName": "run",
            "args": [
                "${workspaceRoot}\project.json"
            ],
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}
  • Try the trick
    Press Ctrl+Shift+B
    Output:
Project NetApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hello World!

Useful User Settings

  • Auto save
    Select File -> Preferences -> User Settings
// Place your settings in this file to overwrite the default settings
{
    "files.autoSave": "afterDelay"
}
原文地址:https://www.cnblogs.com/steven-yang/p/5867662.html