如何在Microsoft的新Visual Studio代码中编译c#?

时间:2023-01-16 15:39:14

I have installed the preview version of Microsoft's new code editor "Visual Studio Code". It seems quite a nice tool! The introduction mentions you can program c# with it, but the setup document does not mention how to actually compile c# files. You can define "mono" as a type in the "launch.json" file, but that does not do anything yet. Pressing F5 results in: "make sure to select a configuration from the launch dropdown"... Also, intellisense is not working for c#? How do you set the path to any included frameworks?

我安装了Microsoft新代码编辑器“Visual Studio Code”的预览版。这似乎是一个很好的工具!介绍中提到你可以使用它编写c#,但是安装文档没有提到如何实际编译c#文件。您可以将“mono”定义为“launch.json”文件中的类型,但这还没有做任何事情。按F5会导致:“确保从启动下拉列表中选择一个配置”...此外,intellisense不适用于c#?如何设置任何包含框架的路径?

Launch.json:

"configurations": [
        {
            // Name of configuration; appears in the launch configuration drop down menu.
            "name": "Cars.exe",
            // Type of configuration. Possible values: "node", "mono".
            "type": "mono",
            // Workspace relative or absolute path to the program.
            "program": "cars.exe",
        }, 
        {
            "type": "mono",
        }

4 个解决方案

#1


Intellisense does work for C# 6, and it's great.

Intellisense确实适用于C#6,它很棒。

For running console apps you should set up some additional tools:

要运行控制台应用程序,您应该设置一些其他工具:

  • ASP.NET 5; in Powershell: &{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}
  • ASP.NET 5;在Powershell中:&{$ Branch ='dev'; iex((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}

  • Node.js including package manager npm.
  • Node.js包括包管理器npm。

  • The rest of required tools including Yeoman yo: npm install -g yo grunt-cli generator-aspnet bower
  • 剩下的必备工具包括Yeoman哟:npm install -g yo grunt-cli generator-aspnet bower

  • You should also invoke .NET Version Manager: c:\Users\Username\.dnx\bin\dnvm.cmd upgrade -u
  • 您还应该调用.NET版本管理器:c:\ Users \ Username \ .dnx \ bin \ dnvm.cmd upgrade -u

Then you can use yo as wizard for Console Application: yo aspnet Choose name and project type. After that go to created folder cd ./MyNewConsoleApp/ and run dnu restore

然后,您可以使用yo作为控制台应用程序的向导:yo aspnet选择名称和项目类型。之后转到创建文件夹cd ./MyNewConsoleApp/并运行dnu restore

To execute your program just type >run in Command Palette (Ctrl+Shift+P), or execute dnx . run in shell from the directory of your project.

要执行程序,只需在命令选项板中键入>运行(Ctrl + Shift + P),或执行dnx。从项目目录中运行shell。

#2


Since no one else said it, the short-cut to compile (build) a C# app in Visual Studio Code (VSCode) is SHIFT+CTRL+B.

由于没有其他人说过,在Visual Studio Code(VSCode)中编译(构建)C#应用程序的快捷方式是SHIFT + CTRL + B.

If you want to see the build errors (because they don't pop-up by default), the shortcut is SHIFT+CTRL+M.

如果要查看构建错误(因为它们默认不弹出),快捷方式是SHIFT + CTRL + M.

(I know this question was asking for more than just the build shortcut. But I wanted to answer the question in the title, which wasn't directly answered by other answers/comments.)

(我知道这个问题不只是要求构建快捷方式。但我想回答标题中的问题,而其他答案/评论没有直接回答。)

#3


Install the extension "Code Runner". Check if you can compile your program with csc (ex.: csc hello.cs). The command csc is shipped with Mono. Then add this to your VS Code user settings:

安装扩展“Code Runner”。检查是否可以使用csc编译程序(例如:csc hello.cs)。命令csc随Mono一起提供。然后将其添加到您的VS Code用户设置:

"code-runner.executorMap": {
        "csharp": "echo '# calling mono\n' && cd $dir && csc /nologo $fileName && mono $dir$fileNameWithoutExt.exe",
        // "csharp": "echo '# calling dotnet run\n' && dotnet run"
    }

Open your C# file and use the execution key of Code Runner.

打开C#文件并使用Code Runner的执行键。

Edit: also added dotnet run, so you can choose how you want to execute your program: with Mono, or with dotnet. If you choose dotnet, then first create the project (dotnet new console, dotnet restore).

编辑:还添加了dotnet运行,因此您可以选择执行程序的方式:使用Mono或使用dotnet。如果选择dotnet,则首先创建项目(dotnet new console,dotnet restore)。

#4


SHIFT+CTRL+B should work

SHIFT + CTRL + B应该有效

However sometimes an issue can happen in a locked down non-adminstrator evironment:

但是,有时在锁定的非管理员环境中会出现问题:

If you open an existing C# application from the folder you should have a .sln (solution file) etc..

如果从文件夹中打开现有的C#应用​​程序,则应该有.sln(解决方案文件)等。

Commonly you can get these message in VS Code

通常,您可以在VS Code中获取这些消息

Downloading package 'OmniSharp (.NET 4.6 / x64)' (19343 KB) .................... Done!
Downloading package '.NET Core Debugger (Windows / x64)' (39827 KB) .................... Done!

Installing package 'OmniSharp (.NET 4.6 / x64)'
Installing package '.NET Core Debugger (Windows / x64)'

Finished
Failed to spawn 'dotnet --info'  //this is a possible issue

To which then you will be asked to install .NET CLI tools

然后,您将被要求安装.NET CLI工具

If impossible to get SDK installed with no admin privilege - then use other solution.

如果无法在没有管理员权限的情况下安装SDK,那么请使用其他解决方案。

#1


Intellisense does work for C# 6, and it's great.

Intellisense确实适用于C#6,它很棒。

For running console apps you should set up some additional tools:

要运行控制台应用程序,您应该设置一些其他工具:

  • ASP.NET 5; in Powershell: &{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}
  • ASP.NET 5;在Powershell中:&{$ Branch ='dev'; iex((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}

  • Node.js including package manager npm.
  • Node.js包括包管理器npm。

  • The rest of required tools including Yeoman yo: npm install -g yo grunt-cli generator-aspnet bower
  • 剩下的必备工具包括Yeoman哟:npm install -g yo grunt-cli generator-aspnet bower

  • You should also invoke .NET Version Manager: c:\Users\Username\.dnx\bin\dnvm.cmd upgrade -u
  • 您还应该调用.NET版本管理器:c:\ Users \ Username \ .dnx \ bin \ dnvm.cmd upgrade -u

Then you can use yo as wizard for Console Application: yo aspnet Choose name and project type. After that go to created folder cd ./MyNewConsoleApp/ and run dnu restore

然后,您可以使用yo作为控制台应用程序的向导:yo aspnet选择名称和项目类型。之后转到创建文件夹cd ./MyNewConsoleApp/并运行dnu restore

To execute your program just type >run in Command Palette (Ctrl+Shift+P), or execute dnx . run in shell from the directory of your project.

要执行程序,只需在命令选项板中键入>运行(Ctrl + Shift + P),或执行dnx。从项目目录中运行shell。

#2


Since no one else said it, the short-cut to compile (build) a C# app in Visual Studio Code (VSCode) is SHIFT+CTRL+B.

由于没有其他人说过,在Visual Studio Code(VSCode)中编译(构建)C#应用程序的快捷方式是SHIFT + CTRL + B.

If you want to see the build errors (because they don't pop-up by default), the shortcut is SHIFT+CTRL+M.

如果要查看构建错误(因为它们默认不弹出),快捷方式是SHIFT + CTRL + M.

(I know this question was asking for more than just the build shortcut. But I wanted to answer the question in the title, which wasn't directly answered by other answers/comments.)

(我知道这个问题不只是要求构建快捷方式。但我想回答标题中的问题,而其他答案/评论没有直接回答。)

#3


Install the extension "Code Runner". Check if you can compile your program with csc (ex.: csc hello.cs). The command csc is shipped with Mono. Then add this to your VS Code user settings:

安装扩展“Code Runner”。检查是否可以使用csc编译程序(例如:csc hello.cs)。命令csc随Mono一起提供。然后将其添加到您的VS Code用户设置:

"code-runner.executorMap": {
        "csharp": "echo '# calling mono\n' && cd $dir && csc /nologo $fileName && mono $dir$fileNameWithoutExt.exe",
        // "csharp": "echo '# calling dotnet run\n' && dotnet run"
    }

Open your C# file and use the execution key of Code Runner.

打开C#文件并使用Code Runner的执行键。

Edit: also added dotnet run, so you can choose how you want to execute your program: with Mono, or with dotnet. If you choose dotnet, then first create the project (dotnet new console, dotnet restore).

编辑:还添加了dotnet运行,因此您可以选择执行程序的方式:使用Mono或使用dotnet。如果选择dotnet,则首先创建项目(dotnet new console,dotnet restore)。

#4


SHIFT+CTRL+B should work

SHIFT + CTRL + B应该有效

However sometimes an issue can happen in a locked down non-adminstrator evironment:

但是,有时在锁定的非管理员环境中会出现问题:

If you open an existing C# application from the folder you should have a .sln (solution file) etc..

如果从文件夹中打开现有的C#应用​​程序,则应该有.sln(解决方案文件)等。

Commonly you can get these message in VS Code

通常,您可以在VS Code中获取这些消息

Downloading package 'OmniSharp (.NET 4.6 / x64)' (19343 KB) .................... Done!
Downloading package '.NET Core Debugger (Windows / x64)' (39827 KB) .................... Done!

Installing package 'OmniSharp (.NET 4.6 / x64)'
Installing package '.NET Core Debugger (Windows / x64)'

Finished
Failed to spawn 'dotnet --info'  //this is a possible issue

To which then you will be asked to install .NET CLI tools

然后,您将被要求安装.NET CLI工具

If impossible to get SDK installed with no admin privilege - then use other solution.

如果无法在没有管理员权限的情况下安装SDK,那么请使用其他解决方案。