All pastes #792330 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-11-21 09:19 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public Process p;
        public TextReader t;
        public Form1()
        {
            InitializeComponent();
            if ( !Directory.Exists( @"out" ) ) {
                Directory.CreateDirectory( @"out" );
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Clear it every time we click the button :D
            txtMain.Text = "";
            string CSType = "";
            //Make sure we've got a directory!
            if (folderBrowserDialog1.SelectedPath == "")
            {
                txtMain.Text += "Please select a folder!";
                return;
            }

            //Make sure we've got an output file! (and check if it's a dll or exe)
            if (txtOut.Text != "" && txtOut.Text.ToLower().EndsWith(".dll") || txtOut.Text.ToLower().EndsWith(".exe"))
            {
                txtMain.Text += "Building ";
                if ( txtOut.Text.ToLower().EndsWith( ".dll" ) )
                {
                    txtMain.Text += "library file";
                    CSType = "dll";
                }
                if (txtOut.Text.ToLower().EndsWith(".exe"))
                    txtMain.Text += "executable file";
                txtMain.Text += " ( " + txtOut.Text + " ) \r\n";
            }
            else {
                txtMain.Text += "Error:\r\nInvalid output filename :(";
                return;
            }
            //Make sure we've only got 1 period!
            string[] splits = txtOut.Text.Split('.');
            if (splits.Length > 2) {
                txtMain.Text += "Error:\r\nPlease only use 1 period in the filename!\r\n";
                return;
            }

            String FLocation = folderBrowserDialog1.SelectedPath;

            //Get location of csc.exe (will change)
            t = new StreamReader("settings.cfg");
            String Settings = t.ReadLine();
            String CSLocation;

            if (Settings == null)
            {
                txtMain.Text += "Error:\r\nInvalid Config Setting\r\n";
                return;
            }

            if (Settings.StartsWith("Location: ") && Settings.EndsWith("\\csc.exe"))
            {
                CSLocation = Settings.Remove(0, 10);
                txtMain.Text += "Loading Directory:\r\n" + FLocation + "\r\n\r\n";
            }
            else {
                txtMain.Text += "Error:\r\nCould not read config :(\r\n";
                return;
            }

            DirectoryInfo DIR = new DirectoryInfo( FLocation );
            
            FileInfo[] files = DIR.GetFiles("*.cs");
            if (files.Length == 0)
            {
                txtMain.Text += "Error:\r\nCouldn't locate any .cs files :(\r\n";
                return;
            }
            txtMain.Text += "Located Files:\r\n";
            foreach(FileInfo fi in files){
                txtMain.Text += fi.Name + "\r\n";
            }
            string Arguments = "";
            //Now we create the argument to pass to csc.exe
            if (cbDebug.CheckState == CheckState.Checked) //Are we debugging?
                Arguments = Arguments + "/define:DEBUG ";
            if (cbOpt.CheckState == CheckState.Checked) //Are we optimizing?
                Arguments = Arguments + "/optimize ";
            if (CSType == "dll")
                Arguments = Arguments + "/target:library ";
            //Add output file
            Arguments = Arguments + "/out:\"" + System.Environment.CurrentDirectory + "/out/" + txtOut.Text + "\" /nologo \"" + FLocation + "\\*.cs\"";
            
            try
            {
                p = new Process();
                p.StartInfo.FileName = CSLocation;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.Arguments = Arguments;
                //Create a StreamReader Here And output error to txtMain
                p.Start();
                p.WaitForExit();
            }
            catch (FileNotFoundException)
            {
                txtMain.Text += "Error:\r\ncsc.exe not found!\r\n";
            }

            //Check if compiled.
            if ( File.Exists( System.Environment.CurrentDirectory + "\\out\\" + txtOut.Text ) )
                txtMain.Text += "Success!\r\nCompiled to \\out\\" + txtOut.Text + "\r\n";
            else
                txtMain.Text += "Error\r\nSomething catastrophic!\r\n";

        }

        private void txtOut_MouseHover(object sender, EventArgs e)
        {
            if (txtOut.Text == "Output File Name:")
                txtOut.Text = "";
        }

        private void txtOut_MouseLeave(object sender, EventArgs e)
        {
            if (txtOut.Text == "")
                txtOut.Text = "Output File Name:";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
        }
    }
}