简体   繁体   English

无法在Visual Studio中使用dotnet run命令运行代码。 “找不到要运行的项目”

[英]Can't run code using dotnet run command in visual studio. “Couldn't find a project to run”

I have tried running this program a million times. 我已尝试运行此程序一百万次。 I am in the correct directory C:\\Users\\DylanT\\Desktop\\Project and the file is just named Program.cs when i use the dotnet run command i get the error Couldn't find a project to run. Ensure a project exists in C:\\Users\\DylanT\\Desktop\\Project, or pass the path to the project using --project. 我在正确的目录C:\\Users\\DylanT\\Desktop\\Project ,当我使用dotnet run命令时,该文件仅命名为Program.cs。我收到错误消息: Couldn't find a project to run. Ensure a project exists in C:\\Users\\DylanT\\Desktop\\Project, or pass the path to the project using --project. Couldn't find a project to run. Ensure a project exists in C:\\Users\\DylanT\\Desktop\\Project, or pass the path to the project using --project.

I only have one folder named "Project" and the only file inside the folder is the program so there should be no reason it shouldn't execute the program. 我只有一个名为“ Project”的文件夹,并且文件夹内的唯一文件是程序,因此应该没有理由不执行该程序。 Any ideas? 有任何想法吗?

As you can read here you have to call the command on a .csproj-file. 如您在这里所读您必须在.csproj文件上调用命令。 You simply can not call it on a source code file. 您根本无法在源代码文件上调用它。

The .NET Build System does not work with loose .cs files (or f# or vb for that matter), it needs at least a Project . .NET Build System不适用于松散的.cs文件(或者f#或vb),它至少需要一个Project You can generate a Project file by using dotnet new <template> as follows: 您可以使用dotnet new <template>生成一个Project文件 ,如下所示:

md MyProject
cd MyProject
dotnet new console

Now you have a folder MyProject , containing MyProject.csproj and a default Program.cs file. 现在,您有一个文件夹MyProject ,其中包含MyProject.csproj和一个默认的Program.cs文件。

To run it, you can now do 要运行它,您现在可以做

dotnet run

This will open and read the .csproj file, it will build all the sourcefiles and packages that are referenced inside it into an executable, and then it will run that. 这将打开并读取.csproj文件,它将内部引用的所有源文件和包构建为可执行文件,然后运行该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM