C# Scripter: How to execute C# as batch files with my new utility – level 200

UPDATE:  This utility has been upgraded to support more .Net languages.  See here for the newer version.

The Windows shell has some great programming capabilities, and we can do a lot with it, but these days, I can whip something out with C# and basically do anything I need to with files.  It takes me considerably longer to do the same thing with DOS commands (mostly because I don’t know advanced shell scripting).  When Monad comes out in the next version of Windows, we’ll have .Net integrated into the shell, but that is many years away.  I’m left wanting to execute C# batch-style.

It’s no problem to create a console app and run it, but I want to have the .cs file available for tweaking, so it’s not feasible to just create a bunch of .exe files.  I needed a true C# scripting solution.

I now introduce the Palermo C# Scripter.  On the command line, it’s csscripter.exe, and it takes as an argument the name of the C# file.  For example:  csscripter.exe myBatch.cs

myBatch.cs just has be a valid console class like the following:

    1 using System;

    2 using System.Collections;


    3 using System.IO;


    4 using System.Windows.Forms;


    5 


    6 public class Whatever


    7 {


    8     public static void Main(string[] args)


    9     {


   10         Console.WriteLine(“Other Script file executed. ” + string.Join(“|”, args));


   11         MessageBox.Show(“You can even do pop-ups”, “With a title”);


   12     }


   13 }

I’m using this now to traverse thousands of nested files searching for little things and doing small things when I find something.  This is the same stuff I’d do with batch files, but now I have the power of C# in text form doing it for me. 

You can download the utility from me here.

If you have feedback or a feature request, please leave me a comment.  In putting this together, I came across some good references on C# scripting:

13 comments

  1. If there is interest in the code, I’d be willing to open-source it. Just leave me a comment.

  2. Nice utility! I have a need for something like this right now and was going to write one. I would be interested in the source for sure, it would certainly save some time and provide some enhancements too.

  3. Jeremy D. Miller

    The very first thing you should do with C# Scripter is apply it to a certain codebase Monday morning so our solution doesn’t contain 50 projects anymore.

  4. Brendan Tompkins

    Oh, this is very cool!

  5. I haven’t compared it to NScript, but it solves the same problem. I believe one advantage I have is that it’s just a stand-along executable. You don’t have to execute the .msi to install it on the machine. You just need the .EXE to use it (much like NAnt, NUnit, etc). I don’t claim that this is an orginial idea, and there have been others that wrote a similar tool (see the links in my post).

  6. Ooh, nice. Very nice! I can already think of a few things I’ve been putting off simply because I don’t want a full solution and exe floating around just to do simple things.

    Try not to point and laugh at the mort, but… any chance it can do .vb files?

  7. Daniel,
    No, this is C# -specific. It invokes the C# compiler internally.

  8. hehe, no probs. Guess I’d better brush up my c# skills to get stuff done. Thanks for the quick reply, much appreciated 🙂

  9. Andy

    Depending on how you implemented it, I don’t think that it would be very hard to add VB support. If you use CodeDom to compile the the passed in file, I think all that would need to be done is to create an instance of the correct CodeProvider (VB vs C#) based on the file that was passed in. Maybe the code could decide based on the file extension?

  10. Andy,
    Yes, it definitely could be made to compile VB as well. I’ll have to think about adding that. I have no use for that, but other people may.

  11. I’ve modified the scripter so that it now supports VB and JScript. I’ll have to rename it to netscripter.exe. I’ll post it soon.

Comments are closed.