【发布时间】:2021-02-19 20:44:27
【问题描述】:
安装 Vscode 后,我做了以下操作:
- 我在 vscode 中打开了一个空文件夹。
- 为 Visual Studio 代码添加了 C#。
- 我在终端输入
dotnet new console命令创建bin、obj文件夹、.csproj文件和Program.cs文件。 - 单击命令面板上的 .Net Generate Assets for Build and Debug 并生成包含 launch.json 和 tasks.json 文件的 .vscode。
- 我将 launch.json 文件的“控制台”部分更改为 externalTerminal。
- 在我的工作区中,我又创建了一个名为 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