How to compile C # without VS

less than 1 minute read

Verification environment

・ Windows10 64bit 1909 build 18363.1082
-DotnetFramework x64 v4.0.30319

Reference (or almost plagiarism …)

https://qiita.com/toshirot/items/dcf7809007730d835cfc

procedure

① Create the following sample cs file

test.cs


using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing;

class Neko : Form {

  [STAThread]
  public static void Main() {
    GeneratedCodeAttribute generatedCodeAttribute = new GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator","10.0.0.0);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run( new Neko());
  }

  Neko(){
    Text = "Button Click Sample";
    ClientSize = new Size(200, 200);
    Button btn1 = new Button();
    TextBox txb1 = new TextBox();
    Label lb1 = new Label();
    btn1.Location = new Point(50, 50);
    txb1.Location = new Point(100,100);
    lb1.Location = new Point(150,150);
    
    btn1.Text = "Click!";
    btn1.Click += btn1_Click;
    
    lb1.Text = "test";
    
    Controls.AddRange(new Control[] { btn1 });
    Controls.AddRange(new Control[] { txb1 });
    Controls.AddRange(new Control[] { lb1 });
  }

  void btn1_Click(object sender, System.EventArgs e) {
    MessageBox.Show("Hello Meow", "Greeting");
  }
}

2 Start the command prompt and execute the following code

Run


C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe D:\test\test.cs

(3) “test.exe” is spit out in the current directory of cmd, so execute it.
image.png

Tags:

Updated: