Scripting for C#

Have you ever wanted to quickly run a C# application without having to setup a new project in Visual Studio and configure all the settings? A fellow developer here at Microsoft has written a tool called Code Runner .NET that allows just that. It isn't scripting exactly because the code is still compiled before being run instead of being interpreted, but you don't have to maintain project or solution files or worry about binaries. 

To try it out I downloaded the tool and installed it to c:\temp\csr. I created a file test.csr after referring to the Getting Started Guide:

    1: using System;
    2: using Microsoft.Tools.CodeRunner;
    4: public class Program
    5: {
    6:     public static int Main(string[] args)
    7:     {
    8:         if (args.Length == 0)
    9:         {
   10:             // TODO: Fill out usage information
   11:             Console.WriteLine("Usage: {0}", ScriptEnvironment.ScriptPath.FileAndExtension);
   12:             return 0;
   13:         }
   15:         // TODO: Script code goes here...
   17:         return 0;
   18:     }
   19: }

My directory was completely empty aside from this file:

C:\temp\cr\mytest>dir /b

I could then run the file as follows:

C:\temp\cr\mytest>..\csr test.csr
Usage: test.csr

That's it. The directory was still clean after the run:

C:\temp\cr\mytest>dir /b

You can also debug your code in Visual Studio using a a nifty tool called scaffold which makes the requisite csproj and sln files and conveniently cleans up after we're done:

C:\temp\cr\mytest>..\Scaffold.exe test.csr
Response file 'C:\temp\cr\csc.rsp' processed
File 'C:\temp\cr\mytest\Scaffold_78D592CA\test.csproj' created
File 'C:\temp\cr\mytest\Scaffold_78D592CA\test.csproj.user' created
File 'C:\temp\cr\mytest\Scaffold_78D592CA\test.sln' created
Starting Visual Studio
Waiting for Visual Studio to exit

We can now debug as usual:


When Visual Studio closes:

Sub-directory 'C:\temp\cr\mytest\Scaffold_78D592CA\' was deleted

Code Runner .NET has already been updated to support Visual Studio 2008 so download it from CodePlex now.