using System; using System.IO; using System.Text; using System.Diagnostics; class Script { static string classStart = "namespace Scripting\r\n" + "{\r\n" + " class ClassLesssSript\r\n" + " {\r\n" + " static public void Main(string[] args)\r\n" + " {\r\n"; static string classEnd = " }\r\n" + " }\r\n" + "}\r\n"; const string usage = "Usage: cscscript classLess file...\nExecutes C# script file that contains free standing C# code without any class declarations.\n Example: cscscript classLess script.cs\n"; static public void Main(string[] args) { if (args.Length == 0 || (args.Length == 1 && (args[0] == "?" || args[0] == "/?" || args[0] == "-?" || args[0].ToLower() == "help"))) { Console.WriteLine(usage); } else { if (args.Length > 0) { string oldFile = ResolveFile(args[0]); string newFile = Path.GetTempFileName(); //if (File.Exist() //) try { using (StreamReader sr = new StreamReader(oldFile)) { using (StreamWriter sw = new StreamWriter(newFile)) { bool header = true; bool firstLine = true; string line; while ((line = sr.ReadLine()) != null) { if (firstLine) { firstLine = false; sw.WriteLine("using System;"); //default "using..." if (!line.StartsWith("using ")) //there is no header { sw.WriteLine(classStart); header = false; } } if (header && line == "") //end of the header { sw.WriteLine(classStart); header = false; } sw.WriteLine(line); } sw.WriteLine(classEnd); } } StringBuilder newArgs = new StringBuilder(); newArgs.Append("\"" + newFile + "\" "); for (int i=1; i