tags:

views:

124

answers:

5

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  A: 
> 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
leppie
Thats a really cool solution.
acidzombie24
I might very well do this if i get sick of writing namespaces or if i feel like having built in functions.
acidzombie24
+2  A: 

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

Haven't used it much myself though.

Spike
+3  A: 

Mono has CsharpRepl which is an interactive shell for C#

Jack Nock
+1  A: 

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

Sharique
+1  A: 

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.

Don