using System; using System.Diagnostics; using System.IO; using System.Windows.Forms; namespace CSharpCompilerGUI { public partial class MainForm : Form { private TextBox sourceCodeTextBox; private TextBox outputTextBox; private Button compileButton; private Label label1; private Label label2; private TextBox compilerPathTextBox; private Button setCompilerPathButton; private Label label3; private string compilerPath; public MainForm() { InitializeComponent(); } private void InitializeComponent() { sourceCodeTextBox = new TextBox(); //outputTextBox = new TextBox(); compileButton = new Button(); label1 = new Label(); //label2 = new Label(); compilerPathTextBox = new TextBox(); setCompilerPathButton = new Button(); label3 = new Label(); SuspendLayout(); sourceCodeTextBox.Location = new System.Drawing.Point(12, 29); sourceCodeTextBox.Name = "sourceCodeTextBox"; sourceCodeTextBox.Size = new System.Drawing.Size(260, 20); sourceCodeTextBox.TabIndex = 0; /* outputTextBox.Location = new System.Drawing.Point(12, 78); outputTextBox.Name = "outputTextBox"; outputTextBox.Size = new System.Drawing.Size(260, 20); outputTextBox.TabIndex = 1; */ compileButton.Location = new System.Drawing.Point(12, 110); compileButton.Name = "compileButton"; compileButton.Size = new System.Drawing.Size(75, 23); compileButton.TabIndex = 2; compileButton.Text = "Compile"; compileButton.UseVisualStyleBackColor = true; compileButton.Click += new System.EventHandler(this.compileButton_Click); label1.AutoSize = true; label1.Location = new System.Drawing.Point(12, 13); label1.Name = "label1"; label1.Size = new System.Drawing.Size(73, 13); label1.TabIndex = 3; label1.Text = "Source Code:"; /* label2.AutoSize = true; label2.Location = new System.Drawing.Point(12, 62); label2.Name = "label2"; label2.Size = new System.Drawing.Size(85, 13); label2.TabIndex = 4; label2.Text = "Output File Path:"; */ compilerPathTextBox.Location = new System.Drawing.Point(12, 78); compilerPathTextBox.Name = "compilerPathTextBox"; compilerPathTextBox.Size = new System.Drawing.Size(260, 20); compilerPathTextBox.TabIndex = 5; setCompilerPathButton.Location = new System.Drawing.Point(12, 137); setCompilerPathButton.Name = "setCompilerPathButton"; setCompilerPathButton.Size = new System.Drawing.Size(75, 23); setCompilerPathButton.TabIndex = 6; setCompilerPathButton.Text = "Set"; setCompilerPathButton.UseVisualStyleBackColor = true; setCompilerPathButton.Click += new System.EventHandler(this.SetCompilerPathButton_Click); label3.AutoSize = true; label3.Location = new System.Drawing.Point(12, 62); label3.Name = "label3"; label3.Size = new System.Drawing.Size(80, 13); label3.TabIndex = 7; label3.Text = "Compiler Path:"; AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ClientSize = new System.Drawing.Size(284, 170); Controls.Add(label3); Controls.Add(setCompilerPathButton); Controls.Add(compilerPathTextBox); Controls.Add(label2); Controls.Add(label1); Controls.Add(compileButton); //Controls.Add(outputTextBox); Controls.Add(sourceCodeTextBox); Name = "MainForm"; Text = "C# Compiler GUI"; MaximizeBox = false; ResumeLayout(false); PerformLayout(); } private void compileButton_Click(object sender, EventArgs e) { string sourceCodePath = "\"" + sourceCodeTextBox.Text + "\""; string outputFilePath = "\"" + outputTextBox.Text + "\""; CompileCode(sourceCodePath, outputFilePath); } private void CompileCode(string sourceCodePath, string outputFilePath) { Process process = new Process(); process.StartInfo.FileName = compilerPath; process.StartInfo.Arguments = sourceCodePath; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; // 標準エラー出力をリダイレクトする process.StartInfo.UseShellExecute = false; process.Start(); process.WaitForExit(); string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); // エラーメッセージを取得 if (process.ExitCode == 0) { MessageBox.Show("コンパイルが成功しました。"); // コンパイルが成功した場合、出力ファイルのパスを変更する string compiledFilePath = outputFilePath; } else { MessageBox.Show("コンパイル中にエラーが発生しました。\n\nエラーメッセージ:\n" + output); } } private void SetCompilerPathButton_Click(object sender, EventArgs e) { compilerPath = compilerPathTextBox.Text; MessageBox.Show("コンパイラのパスが設定されました。"); } } static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } }