【问题标题】:How can I run several C# files in the same workspace separately?如何在同一个工作区中分别运行多个 C# 文件?
【发布时间】:2021-02-19 20:44:27
【问题描述】:

安装 Vscode 后,我做了以下操作:

  1. 我在 vscode 中打开了一个空文件夹。
  2. 为 Visual Studio 代码添加了 C#。
  3. 我在终端输入dotnet new console命令创建bin、obj文件夹、.csproj文件和Program.cs文件。
  4. 单击命令面板上的 .Net Generate Assets for Build and Debug 并生成包含 launch.json 和 tasks.json 文件的 .vscode。
  5. 我将 launch.json 文件的“控制台”部分更改为 externalTerminal。
  6. 在我的工作区中,我又创建了一个名为 test 的 c# 文件。

之后,当我尝试运行 test.cs 文件时,出现 CS0017 错误。后来,当我将 Main 方法重命名为 test 文件中的其他内容时,错误消失了。但是,当我运行测试文件时,它是使用 dotnew new console 命令创建的 Program.cs 文件。我在谷歌上搜索了如何做到这一点并重新安装了 Vscode,但无法修复它。

这里是 Program.cs 文件和 test.cs 文件和 launch.json 文件

Program.cs

using System;

namespace ProgramFile
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! Program");
            Console.ReadKey();
        }
    }
}

test.cs

using System;

namespace testFile
{
    class test
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World! Test");
            Console.ReadKey();
        }
    }
}

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 Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/Vscode.Workspace.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "externalTerminal",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

我的 VSocde My Vscode picture please take a look

有没有办法在一个工作区中创建多个 C# 文件,并且每次只运行你想运行的文件?

【问题讨论】:

  • 一个C#程序只能有一个入口点,按照惯例由方法签名static void Main()决定。我不确定,为什么一个控制台应用需要两个入口点?
  • 如果您正在制作两个不同的程序,请创建第二个项目。
  • 你不“运行”C#文件,你将它们编译成可执行文件并运行可执行文件。
  • 如何制作第二个程序?我想在同一个工作区中处理多个程序,但是当我创建并运行这样的新文件时,只有通过“dotnet new console”命令创建的文件才会继续运行。如果您知道如何在同一个工作空间中创建两个程序文件并在必要时调试或运行它们,请告诉我。

标签: c# visual-studio-code


【解决方案1】:

转到项目 -> 属性 -> 应用程序选项卡 -> 启动对象

【讨论】:

  • 感谢您的回答!但是您知道如何打开“应用程序”选项卡或“属性”在哪里吗?这是我第一次使用 VScode。我使用 VScode 应用程序选项卡进行了搜索,但我无法获得任何信息。
  • 在 .csproj 文件下查找“启动对象”。
  • 我想说我真的很抱歉。我不知道我是否找不到它,但我找不到你说的。我会在问题中放一张我的 VScode 的图片。如果可能,请告诉我是哪个文件。
  • 如果 .csproj 文件中没有它,您可以添加属性组 -> {Namespace.className}
  • 谢谢。所以你的意思是添加一个 {Namespace 我要运行的文件。我想在使用“”命令创建的 .csproj 文件中的 之间运行的文件的类名。你的意思是?
猜你喜欢
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-14
  • 1970-01-01
  • 2019-06-14
相关资源
最近更新 更多