9

I am trying to run a hello world C# code from command line and this is the batch file. Tried searching for it but most of the questions are about the XML file reader, I am not reading any XML files.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "E:\a\c.cs" /p:Configuration=Debug;DeployOnBuild=True;PackageAsSingleFile=False;outdir=E:\a

and this is c.cs

class c {
    public static void Main() {
        System.Console.Clear();
        System.Console.WriteLine("hey");
        System.Console.ReadKey();
    }
}

and the error

enter image description here

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Achshar
  • 5,153
  • 8
  • 40
  • 70
  • Text editor. Sublime. Yeah both bat and c.cs are in same directory. Otherwise the error would be different I guess, it definitely finds the cs file. – Achshar Nov 06 '15 at 21:18
  • Not sure, but I have been using sublime for all this time writing other command line stuff never had any issues. And when I try to save as it in notepad it shows ANSI which I am assuming is the encoding of the file. – Achshar Nov 06 '15 at 21:28

5 Answers5

13

msbuild expects a project file or a solution file as input:

https://msdn.microsoft.com/en-us/library/ms164311.aspx

What you want to do is run the c# compiler directly:

Compiling/Executing a C# Source File in Command Prompt

Community
  • 1
  • 1
Russell McClure
  • 4,821
  • 1
  • 22
  • 22
  • I am new to .NET, so please bear with me. Will I be able to use the .NET stuff if I compile with c# compiler directly? Basically will it make it different from if I would have done it with visual studio and all the bells and whistles. – Achshar Nov 06 '15 at 21:24
  • Everything should be available to you without having to use a project file. But once you get beyond a simple test console application, you will want to have the help of a project file to manage your different files (.cs, .resx etc) as well as their dependencies. – Russell McClure Nov 06 '15 at 21:36
  • Good lord I been fighting this for over an hour! --Build succeeded.-- Thanks! – Joseph Kreifels II Nov 20 '18 at 18:24
3

If you are running dotnet with the following command while running.

dotnet run --project [project_name].dll

You can run the following instead of the above command.

dotnet [project_name].dll
Furkan Doğanay
  • 93
  • 1
  • 10
0

Close the Visual Studio, going the arquivo projects and delete paste ".Vs" and start the Visual Studio

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 04 '21 at 17:16
0

Check the project file(*.cproj). The file must have the correct Syntax.

M Fa
  • 109
  • 1
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31633422) – Reza Heidari May 02 '22 at 18:36
0

You cannot use a .cs file as an entry point.

In 2022:

  • You can use Developer command prompt for VS 2022(type in start menu)
  • Create a solution/project, include the .cs class in it.
  • cd to the solution folder
  • >dotnet build
  • This will create an .exe or .dll if there are no compiler errors
  • Make sure you are inside the folder with the .csproj file and run the following command.
  • You can add arguments if there's any (dotnet run -- 6)
  • >dotnet run
Savindya A
  • 181
  • 1
  • 6