Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run code using the 'dotnet' run command in Visual Studio. "Couldn't find a project to run"

Tags:

c#

.net-core

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 shouldn't be any reason it shouldn't execute the program. How can I do it?

like image 996
DylanT Avatar asked Sep 18 '25 07:09

DylanT


2 Answers

The .NET Build System does not work with loose .cs files (or F# or VB.NET 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 source files into an executable, and then it will run that.

like image 197
Peter B Avatar answered Sep 20 '25 22:09

Peter B


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.

like image 35
Sancho Panza Avatar answered Sep 20 '25 22:09

Sancho Panza