7

I am doing a little scripting and i find sometime more power would be nice. Like the ability to keep trying to delete a file with a 1sec delay AND have it portable. I spent some time today translating a bat script to bash. I know i can use php or python but i VERY MUCH PREFER static/compile time checking.

Is there a way to run C# code as a script? As long as i can edit the source quickly (like in a notepad) and run it simply then i'll be happy.

I am hoping i dont have to create a custom ext and write a app to dynamically compile and execute the script (i know have source to compile .js somewhere...). Does anyone know of a solution?

3
  • 3
    There's PowerShell if you want access to the .NET library (as well as a bunch of commandlets). F# also has an interactive mode which will give you access to .NET libraries from a console. Either of those seems like a more straight forward solution than compiling C# on the fly. Commented May 28, 2010 at 6:08
  • I am not sure I follow your question. You want to be able to "edit the source quickly and run it", but you "prefer static/compile time checking". Do you want a language that is interpreted or one that is compiled? Commented May 28, 2010 at 7:07
  • you could use either csscript or linqpad as answered. I use csscript for every complex batch Commented Jan 3, 2014 at 8:11

5 Answers 5

6

Well there's CS-Script. http://csscript.net/

Haven't used it much myself though.

Sign up to request clarification or add additional context in comments.

Comments

6
> copy con cs.bat
csc -o OUTPUT.exe %1
OUTPUT.exe
del OUTPUT.exe
^Z
> cs somefile.cs

Here is a better version of a batch file:

@echo off
echo using System; class P { static void Main() { > foo.cs
type %1 >> foo.cs
echo }} >> foo.cs
csc /nologo /out:foo.exe foo.cs
del foo.cs
foo.exe
del foo.exe

So you have some file called foo.txt:

Console.WriteLine("Hello world");

Invoke it like:

cs.bat foo.txt

2 Comments

I might very well do this if i get sick of writing namespaces or if i feel like having built in functions.
@leppie thanks for this, I've used your code as a base for a handy batch file of my own: gist.github.com/3018417
3

Mono has CsharpRepl which is an interactive shell for C#

Comments

2

Whenever I need to execute a small snippet to perform something in C# I just fire up LinqPad.

Perhaps it includes a bit to much for simply small scripts but it beats making a new console project every time.

Comments

1

There is a shell developed by mono project developers name CsharpRepl. I have never tried, but it seems very interesting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.