Delay's Blog is the blog of David Anson, a Microsoft developer who works with the Silverlight, WPF, Windows Phone, and web platforms.
http://dlaa.me/
@DavidAns
A few years ago I found myself spending a lot of time writing batch files to perform a variety of relatively simple tasks. For those who aren't familiar, you can do some surprisingly powerful things with batch files - but it usually involves a lot of trickery and arcane knowledge. I wanted a simpler way of doing things, but I didn't want to create a bunch of compiled executables and lose the elegant, easy-to-modify transparency that batch files offer. These days, I'd probably look to PowerShell for the solution, but back then there was no such thing...
What I did have was the power of .NET and some inspiration from a coworker's tool that was able to take .NET 1.1 source code and execute it "on the fly" without the need for compilation. What I did not have was something similar for .NET 2.0 or access to the source code for that tool (because the author had left the company). So I wrote my own:
============================================ == CSI: C# Interpreter == == Delay (http://blogs.msdn.com/Delay/) == ============================================ Summary ======= CSI: C# Interpreter Version 2009-01-06 for .NET 3.5 http://blogs.msdn.com/Delay/ Enables the use of C# as a scripting language by executing source code files directly. The source code IS the executable, so it is easy to make changes and there is no need to maintain a separate EXE file. CSI (CodeFile)+ (-d DEFINE)* (-r Reference)* (-R)? (-q)? (-c)? (-a Arguments)? (CodeFile)+ One or more C# source code files to execute (*.cs) (-d DEFINE)* Zero or more symbols to #define (-r Reference)* Zero or more assembly files to reference (*.dll) (-R)? Optional 'references' switch to include common references (-q)? Optional 'quiet' switch to suppress unnecessary output (-c)? Optional 'colorless' switch to suppress output coloring (-a Arguments)? Zero or more optional arguments for the executing program The list of common references included by the -R switch is: System.dll System.Data.dll System.Drawing.dll System.Windows.Forms.dll System.Xml.dll PresentationCore.dll PresentationFramework.dll WindowsBase.dll System.Core.dll System.Data.DataSetExtensions.dll System.Xml.Linq.dll CSI's return code is 2147483647 if it failed to execute the program or 0 (or whatever value the executed program returned) if it executed successfully. Examples: CSI Example.cs CSI Example.cs -r System.Xml.dll -a ArgA ArgB -Switch CSI ExampleA.cs ExampleB.cs -d DEBUG -d TESTING -R Notes ===== CSI was inspired by net2bat, an internal .NET 1.1 tool whose author had left Microsoft. CSI initially added support for .NET 2.0 and has now been extended to support .NET 3.0 and .NET 3.5. Separate executables are provided to accommodate environments where the latest version of .NET is not available. Version History =============== Version 2009-01-06 Initial public release Version 2005-12-15 Initial internal release
[Click here to download CSI for .NET 3.5, 3.0, 2.0, and 1.1 - along with the complete source code and test suite.]
Using CSI is quite simple. Here's an example from the release package:
C:\T\CSI>TYPE Samples\HelloWorld.cs using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine("Hello world."); } } C:\T\CSI>CSI.exe Samples\HelloWorld.cs Hello world.
And if you use the included batch files to register the .CSI file type (more on this in the notes below), it's even easier:
C:\T\CSI>TYPE Samples\Greetings.csi using System; public class Greetings { public static void Main(string[] args) { Console.WriteLine("Hello {0}", string.Join(" ", args)); } } C:\T\CSI>Samples\Greetings.csi out there world. Hello out there world.
Notes:
RegisterCSI.cmd
UnregisterCSI.cmd
-R
var
LINQ
CSC.exe
CSI.exe
CSI was a fun project in its day and I've done quite a bit with it that would have been fairly tedious otherwise. Today's world offers plenty of alternatives - but if you're comfortable with C# and prefer to stick to one language for all your programming needs, CSI is pretty handy to have around. Because you never know when the urge to code will strike! :)