Can't run code using dotnet run command in visual studio. "Couldn't find a project to run"

Viewed 11297

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.

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. Any ideas?

2 Answers

The .NET Build System does not work with loose .cs files (or f# or vb for that matter), it needs at least a Project. You can generate a Project file by using dotnet new <template> as follows:

md MyProject
cd MyProject
dotnet new console

Now you have a folder MyProject, containing MyProject.csproj and a default Program.cs file.

To run it, you can now do

dotnet run

This will open and read the .csproj file, and it will use the settings and properties that are inside it to build all the sourcefiles into an executable, and then it will run that.

As you can read here you have to call the command on a .csproj-file. You simply can not call it on a source code file.

Related