Auto Compiler


This is a utility to make it easier to compile from the command line. One of the problems with using the command line is you have to remember all the proper command line switches for each file. If you are building a project with several files, this can be confusing. Some third party IDE's will compile for you, but you still have to specify the proper arguments to pass to csc.exe. I got in the habit of adding a comment line at the top of all my files with the proper compilation command line so I could keep track. This utility takes that idea a step further. The program will read that comment line and call the C# compiler for you, passing all the correct parameters automatically.

For the utility to work, all you have to do is add a comment line to each file of the form: //Compile: csc /r:whatever.dll /t:whatever c:\full\path\to\soucefile.cs Note the start of the line must be //Compile: exactly followed by a sigle space. The line can be placed anywhere in the file but near the top will be more efficient. It is important you include the full path to the source file so the program can be run from anywhere and the output will go in the proper directory. When the program is run, you specify the file to compile as an argument. So to compile any file, open a command window in the directory where the file resides and type:   ac filetocompile.cs The program will read and parse the //Compile: line, build the proper command, and call csc to execute the command via a batch file. The output of the compiler is captured in a text file called (with hopefully none) err.txt. Then notepad is run to display the results of the compile. Place the ac.exe assembly somewhere in the system path like c:\windows so the program can run anywhere. The program uses the Process class in System.Diagnostics to execute the C# compiler. The ProcessWindowStyle enumerator is set to Hidden to run the batch file as a background task. 

How To Compile?

csc /r:System.dll /r:System.IO.dll /r:System.Diagnostics.dll /out:ac.exe AutoCompiler.cs

Source Code

using System;
using System.IO;
using System.Diagnostics;
class ac
public static void Main(string[] args)
string filename = args[0];
string line = "";
string command = "";
FileStream fstr =
new FileStream(filename,FileMode.Open, FileAccess.Read); StreamReader str = new StreamReader(fstr);
while(line.StartsWith("//Compile:") != true)
line = str.ReadLine();
string[] tokens = line.Split(new Char[]{' '});
for(int n=2; n<tokens.Length; n++)
command += tokens[n] + " ";
string currpath = Directory.CurrentDirectory;
currpath = currpath + \\comp.bat;
FileStream fs1 =
new FileStream(currpath, FileMode.Create);
StreamWriter sw1 =
new StreamWriter(fs1);
command = "csc " + command + " > err.txt";
sw1.WriteLine("notepad err.txt");
Process p =
new Process();
ProcessStartInfo pinfo =
new ProcessStartInfo();
pinfo.FileName = "comp";
pinfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = pinfo;
catch(Exception e)