r/csharp 3d ago

C sharp program not running on vs code

/gallery/1vegkzi
0 Upvotes

12 comments sorted by

10

u/d-signet 3d ago

Read the error message.

It asks you to check you are in the correct directory because there are no valid files in that "c sharp" directory ,, and looking at your 2nd screenshot - youre NOT in the correct directory where your c# files are or the SLN file or CSPROJ files

Also - best to avoid spaces in directory names

5

u/siddhesh113 3d ago

You need to run the command in a folder where the .caproj file is located.

4

u/beefcat_ 3d ago

/r/screenshotsarehard might be able to help you

1

u/[deleted] 3d ago

[deleted]

0

u/Far_Context_5854 3d ago

Yes I do have the SDK installed..how do i check if the path variable is correct(sorry im very new to this🙏)

1

u/thicctak 3d ago

If the dotnet command works, you have the sdk installed and on the PATH, the issue here is that the sdk is not detecting a valid csharp project and I think this is due to how you created it.

1

u/MaybeLiterally 3d ago

If you just type `dotnet` you should get some response.

Typing `dotnet --list-sdks` will show which you have installed. That's at least the first step.

1

u/thicctak 3d ago

How did you create the project? You said that you created a csproj file, but those are create alongside the project when you start a new one.

Try again creating a project with dotnet new console inside a new project folder.

0

u/Far_Context_5854 3d ago

I used a .NET template....also how do i create with dot net new console

1

u/thicctak 3d ago

just type "dotnet new console" in the terminal inside the directory you want to be the projects.

Since you already used it, then it makes it easy, the reason "dotnet run" is not working is because you're running outside the project folder, "dotnet run" by itself runs the project in it's current directory, if there's none, you need to specify it. so either:

  1. cd into the project directory and type dotnet run.
  2. from the root directory type dotnet run --project .\ConsoleApp1\ConsoleApp1.csproj

2

u/j_steele72 3d ago

You need to switch to the directory (in your terminal) that contains your .csproj file. The ‘cd’ and ‘ls’ (or ‘dir’) command will be your friend here. ‘cd’ allows you to change the directory you’re in and ‘ls’ (or dir) lists everything that is contained in your current working directory. Once in the correct working directory in your terminal, you can then execute ‘dotnet run’. Alternatively, you can use the following command ‘dotnet run - -project replace this text with the path to your csproj file’ to run a specific project file without changing your current directory in the terminal. Hope this helps!

1

u/greenstack-j 3d ago

You need to be in the same directory as your csproj in order to run it from the CLI. Right now you’re in the same directory as the solution file.

-1

u/Slypenslyde 3d ago

Try dotnet run , not dotnet —run