cscの作法 その83

# 概要
cscの作法、調べてみた。
c#をコンパイルして実行してみた。

# 参考にしたページ

https://qiita.com/kimisyo/items/9a554c6021b9e88b81de

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/899910c3-b07a-8013-4c76-0963c9de5736.png)

# コンパイル手順

```
>set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%

>csc pro5.cs
```

# サンプルコード

```


using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;


namespace App
{
	public partial class Form1 : Form {
		private Process currentProcess;
		private StringBuilder outStringBuilder = new StringBuilder();
		private int readCount = 0;
		private Boolean isCanceled = false;
		TextBox textBox3;
		TextBox textBox4;
		Button button1;
		Button button2;
		Button button3;
		public Form1() {
			ClientSize = new Size(500, 500);
			button1 = new Button() {
				Location = new Point(10, 10),
				Size = new Size(90, 20),
				Text = "make",
			}; 
			textBox3 = new TextBox() {
				Location = new Point(10, 40),
				Size = new Size(400, 140),
				Text = "using System;\r\n\r\n\r\npublic class Hello {\r\n    public static void Main(string[] args) {\r\n       System.Windows.Forms.MessageBox.Show(\"hello world!\");\r\n    }\r\n}\r\n",
				Multiline = true,
				ScrollBars = ScrollBars.Both,
			};
			button2 = new Button() {
				Location = new Point(10, 200),
				Size = new Size(90, 20),
				Text = "stop",
			}; 
			textBox4 = new TextBox() {
				Location = new Point(10, 230),
				Size = new Size(400, 140),
				Text = "OK",
				Multiline = true,
				ScrollBars = ScrollBars.Both,
			};
			button3 = new Button() {
				Location = new Point(10, 400),
				Size = new Size(90, 20),
				Text = "run",
			}; 
			Controls.Add(button1);
			Controls.Add(textBox3);
			Controls.Add(button2);
			Controls.Add(textBox4);
			Controls.Add(button3);
			button1.Click += button1_Click;
			button2.Click += button2_Click;
			button3.Click += button3_Click;
		}
		public void AppendText(String data, Boolean console) {
			textBox4.AppendText(data);
			if (console)
			{
				textBox4.AppendText("\r\n");
				Console.WriteLine(data);
			}
		}
		void onExited(object sender, EventArgs e) {
			int exitCode;
			if (currentProcess != null)
			{
				currentProcess.WaitForExit();
				this.Invoke((MethodInvoker)(() => AppendText(outStringBuilder.ToString(), false)));
				outStringBuilder.Clear();
				exitCode = currentProcess.ExitCode;
				currentProcess.CancelOutputRead();
				currentProcess.CancelErrorRead();
				currentProcess.Close();
				currentProcess.Dispose();
				currentProcess = null;
				String pythonFilepath = "a.cs";
				if (File.Exists(pythonFilepath))
				{
					File.Delete(pythonFilepath);
				}
				this.Invoke((MethodInvoker)(() => this.button1.Enabled = true));
				this.Invoke((MethodInvoker)(() => this.button2.Enabled = false));
				if (isCanceled)
				{
					this.Invoke((MethodInvoker)(() => MessageBox.Show("処理をキャンセルしました")));
				}
				else
				{
					if (exitCode == 0)
					{
						this.Invoke((MethodInvoker)(() => MessageBox.Show("処理が完了しました")));
					}
					else
					{
						this.Invoke((MethodInvoker)(() => MessageBox.Show("エラーが発生しました")));
					}
				}
			}
		}
		void p_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) {
			processMessage(sender, e);
		}
		void p_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) {
			processMessage(sender, e);
		}
		void processMessage(object sender, System.Diagnostics.DataReceivedEventArgs e) {
			if (e != null && e.Data != null && e.Data.Length > 0)
			{
				outStringBuilder.Append(e.Data + "\r\n");
			}
			readCount++;
			if (readCount % 5 == 0)
			{
				this.Invoke((MethodInvoker)(() => AppendText(outStringBuilder.ToString(), false)));
				outStringBuilder.Clear();
				if (readCount % 1000 == 0)
				{
					Thread.Sleep(100);
				}
			}
		}
		public void RunCommandLineAsync(String pythonScriptPath) {
			ProcessStartInfo psInfo = new ProcessStartInfo();
			psInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\csc.exe";
			//psInfo.WorkingDirectory = this.textBox3.Text.Trim();
			psInfo.Arguments = "a.cs";
			psInfo.CreateNoWindow = true;
			psInfo.UseShellExecute = false;
			psInfo.RedirectStandardInput = true;
			psInfo.RedirectStandardOutput = true;
			psInfo.RedirectStandardError = true;
			Process p = new Process();
			p.StartInfo = psInfo;
			p.EnableRaisingEvents = true;
			p.Exited += onExited;
			p.OutputDataReceived += p_OutputDataReceived;
			p.ErrorDataReceived += p_ErrorDataReceived;
			p.Start();
			p.BeginOutputReadLine();
			p.BeginErrorReadLine();
			currentProcess = p;
		}
		private void button1_Click(object sender, EventArgs e) {
				String path = "a.cs";
				using (StreamWriter writer = new StreamWriter(path))
				{
					StringReader strReader = new StringReader(this.textBox3.Text.Trim());
					while (true)
					{
						String aLine = strReader.ReadLine();
						if (aLine != null)
						{
							writer.WriteLine(aLine);
						}
						else
						{
							break;
						}
					}
					writer.Close();
				}
				isCanceled = false;
				readCount = 0;
				outStringBuilder.Clear();
				this.Invoke((MethodInvoker)(() => this.textBox4.Clear()));
				RunCommandLineAsync(path);
		}
		private void button2_Click(object sender, EventArgs e) {
			if (currentProcess != null)
			{
				try
				{
					currentProcess.Kill();
					isCanceled = true;
				}
				catch (Exception e2)
				{
					Console.WriteLine(e2);
				}
			}
		}
		private void button3_Click(object sender, EventArgs e) {
			Process.Start("a.exe");
		}
		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}
	}
}





```
以上。