目的は?
visual studioが使えない環境でC#を開発すると都度コンパイルするのが面倒なのでC#でコンパイル楽にするUIを準備しようと思ったのが経緯
ソース
compile_tool.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
[STAThreadAttribute]
static void Main()
{
Application.Run(new Form1());
}
}
class Form1 : Form
{
// 定数
const int fom_num = 5;
//ボタン定義
Button button1;
Button[] buttons;
Button button_tmp;
//テキストボックス定義
TextBox[] txts;
TextBox txt_tmp;
//ラベル定義
Label label1;
//プルダウン
ComboBox combobox1;
//ファイルダイアログ
OpenFileDialog openFileDialog;
//コマンドプロセス用オブジェクト
Process process;
//フォーム全体の定義
public Form1()
{
//フォーム全体設定
this.Width = 500;
this.Height = 210;
this.Text = "C#コンパイルツール";
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
//ソース件数ラベル定義
this.label1 = new Label();
this.label1.Text = "ソース件数";
this.label1.Location = new Point(10, 5);
this.label1.AutoSize = true;
this.Controls.Add(label1);
// ソース件数指定コンボボックス
this.combobox1 = new ComboBox();
this.combobox1.Location = new Point(70, 3);
this.combobox1.Size = new Size(50 , 80);
this.combobox1.Items.AddRange(new object[] {1,2,3,4,5});
this.combobox1.SelectedIndex = 0;
this.Controls.Add(combobox1);
//コンパイル実行ボタン
this.button1 = new Button();
this.button1.Location = new Point(125, 3);
this.button1.Size = new Size(90, 20);
this.button1.Text = "コンパイル実行";
this.button1.Click += new EventHandler(this.Compile_Done);
this.Controls.Add(this.button1);
//ファイルダイアログ表示ボタンと結果格納テキストをループで作成する(5件)
this.txts = new System.Windows.Forms.TextBox[fom_num];
this.buttons = new System.Windows.Forms.Button[fom_num];
for(int i = 0 ; i < fom_num ; i++) {
this.txt_tmp = new TextBox();
this.txt_tmp.Location = new Point(60 , 30 * (i + 1) );
this.txt_tmp.Size = new Size(430 , 20);
this.txt_tmp.Name = "source_" + i;
this.txt_tmp.ReadOnly = true;;
this.Controls.Add(txt_tmp);
this.txts[i] = txt_tmp;
this.button_tmp = new Button();
this.button_tmp.Location = new Point(5 , 30 * (i + 1) );
this.button_tmp.Size = new Size(45 , 20);
this.button_tmp.Name = "source_" + i;
this.button_tmp.Text = "選択";
this.button_tmp.Click += new EventHandler(this.Open_File_Done);
this.Controls.Add(button_tmp);
this.buttons[i] = button_tmp;
}
}
//コンパイルボタン処理
void Compile_Done(object sender, EventArgs e)
{
// コマンド用変数
string cmd_str;
string src_str;
int rtn_cd;
cmd_str = "";
src_str = "";
rtn_cd = 0;
// フルパスをファイル名で置換
cmd_str = @"/c " + System.Reflection.Assembly.GetExecutingAssembly()
.Location.Replace(System.AppDomain.CurrentDomain.FriendlyName, "")
+ "compile.bat";
//コンボボックスに設定された値の回数ループ
for (int i = 0; i < int.Parse(this.combobox1.Text) ; i++){
if(this.txts[i].Text == ""){
MessageBox.Show("選択ソース不足です " + (i + 1) + "番目が空欄" ,"エラー",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
src_str = src_str + this.txts[i].Text + " ";
}
//実行するコマンド
cmd_str = cmd_str + " " + src_str;
//実行コマンド以外の準備
this.process = new System.Diagnostics.Process();
process.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = cmd_str;
//起動
process.Start();
//出力を読み取る
string results = process.StandardOutput.ReadToEnd();
//プロセス終了待機
process.WaitForExit();
//処理終了かつ終わる前に終了コードを取得する
rtn_cd = process.ExitCode;
process.Close();
//出力された結果を表示
if(rtn_cd == 0){
MessageBox.Show("コンパイル完了","終了",MessageBoxButtons.OK,MessageBoxIcon.None);
} else {
MessageBox.Show(results,"コンパイルエラー",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
//選択ボタン処理
void Open_File_Done(object sender, EventArgs e)
{
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "ファイル選択ダイアログ";
openFileDialog.Filter = "C#ソース(*.cs)|*.cs";
openFileDialog.InitialDirectory = @"c:";
//ファイル選択ダイアログを開く
if(openFileDialog.ShowDialog() == DialogResult.OK){
// 対応するテキストボックスを探して代入する
for(int i = 0 ; i < fom_num ; i++){
if(this.txts[i].Name == ((Button)sender).Name){
this.txts[i].Text = openFileDialog.FileName;
}
}
// 設定しない場合は消す
} else {
for(int i = 0 ; i < fom_num ; i++){
if(this.txts[i].Name == ((Button)sender).Name){
this.txts[i].Text = "";
}
}
}
}
}
compile.bat
@echo off
rem インストール先をカレントディレクトリにする
cd %windir%\Microsoft.NET\Framework
rem vから始まるディレクトリを全部取得する
for /d %%a in (v*) do (
set res=%%a
)
rem ソース先をもう少し何とかする
rem set /p soruce=コンパイルするファイル名を入れてください
set pass=%~dp0%soruce%
echo %pass%
rem 書き出し先は固定で良しとする
cd %~dp0
cd コンパイル済
%windir%\Microsoft.NET\Framework\%res%\csc.exe /target:winexe %1 %2 %3 %4 %5
csファイルをコンパイルしてcompile_tool.exeにしたらbatファイルを同じ場所に格納、さらに同じ場所に「コンパイル済」というフォルダを用意する
compile_tool.exeを実行すると以下のようなツールが出るのでC#ソースを選択してコンパイル実行する
compile_tool.csはbatファイルにドラッグアンドドロップすると同じ階層にある「コンパイル済」にコンパイルされるので問題なし。
おわり