1

I created a windows form application from VS 2017. There are 3 files, Program.cs, Form1.Designer.cs, Form1.cs. I want to compile and run it through cmd. I tried to type csc /out:Program.exe Program.cs but it doesn't work.

The error is :

cannot find namespace or class 'Form1'.

The application work in VS2017 but not in cmd. Can anyone tell me how to compile and run this application through cmd?

RBT
  • 24,161
  • 21
  • 159
  • 240
S.C
  • 107
  • 7
  • 1
    You can also ``msbuild`` your solution (.sln) from command line if all you are looking for is a way to automatically build your app. – BitTickler Aug 21 '17 at 04:49
  • 2
    Possible duplicate of [Compiling/Executing a C# Source File in Command Prompt](https://stackoverflow.com/questions/553143/compiling-executing-a-c-sharp-source-file-in-command-prompt) – Thomas Weller Aug 21 '17 at 04:55
  • Especially look at the `/r:AssemblyName.dll` part – Thomas Weller Aug 21 '17 at 04:56
  • 2
    You need to provide all the source files to `csc` – Paweł Łukasik Aug 21 '17 at 04:57
  • [Creating a Simple C# WinForms Application using a Text Editor](https://stackoverflow.com/documentation/winforms/1018/getting-started-with-winforms/11054/creating-a-simple-c-sharp-winforms-application-using-a-text-editor#t=201708210800297494115) – Reza Aghaei Aug 21 '17 at 08:01

1 Answers1

3

You should compile all your classes and not just one on Visual Studio command prompt:

csc.exe *.cs /target:winexe /out:Program.exe 

If you want to create the output exe in bin directory of your project's root directory then you can try below command. You need to ensure that bin directory is already present before firing the command. This command will not create bin directory on its own if not present.

csc.exe  *.cs /target:winexe /out:.\bin\Program.exe

Note: /target switch helps your launch the output exe as a windows forms application. It you don't mention this switch then it will get launched as console application first which in turns lunches the windows application.

An alternative option is to try msbuild command in place of csc command on Visual Studio command prompt:

msbuild WindowsFormsApplication1.csproj

Here you need the project name (*.csproj) in place of individual class files names as command-line parameters. msbuild command also takes care of creating any sub directories like bin, debug if not present.

RBT
  • 24,161
  • 21
  • 159
  • 240