Wednesday, October 04, 2006

Day 20 - Command Line C#

This simple command line application allows you to execute C# code as if it were script. It takes n arguments, where the first argument is the name of the file to execute, and the remaining arguments are passed on to the script.
The purpose of this application is to do things that you can't do in batch files, like download web pages, in an easily modifiable script. All useful imports have been done for you (System.Xml, System.Text, System.Text.Regex, System.IO, etc...).
Just for doing commandline web scraping alone, this is awesome. You could write a 3 line script to download a web page and apply a regex to it.
I know I'll be using this for various things!

Sample File:
{
//show a messagebox
MessageBox.Show(args[0]);

//download from a website
string page = new WebClient().DownloadString(args[1]);
Console.WriteLine(page);

//wait for input
Console.ReadLine();
}

Digg!  Download the app here
  Download the source here

10 Comments:

At 1:17 AM, Anonymous Anonymous said...

Dude, you're it.

I'm so going to manage all my MP3 ID3 and renaming chores in command line now.

I'm admittedly better at JavaScript than I am at anything else, so I'm wondering if that's also an idea? A .js file executer?

 
At 9:32 AM, Anonymous Anonymous said...

how to apply a regex in one line, then?

 
At 9:39 AM, Anonymous Anonymous said...

How does this compare to Alintex Script?

http://www.alintex.com/products.aspx

Just curious... I can see the use of .NET scripting. Already using it to automate some tasks here at work.

 
At 6:50 PM, Anonymous Anonymous said...

If you are looking to run full blown C# files (not fragments) as scripts take a look at http://www.csscript.net/. Oleg has done some amazing work with a lot of neat features. Including shell support for right clicking and running as script, editing in #Develop, editing in VS.Net and so on. Anyway, it is some cool stuff.

 
At 7:03 PM, Anonymous Anonymous said...

For the anonymous users question about RegEx, here you go:
using System;
using System.Text.RegularExpressions;

class Script
{
static public void Main(string[] args)
{
Match m = Regex.Match("StringInput", "(i|n)+");

if (m.Success)
{
Console.WriteLine("RegEx: " + m.ToString());
}
}
}

This is how I do it. There might be easier ways, but this works for me. And as a side note, C# regex was designed to be completley compatible with Perl 5.

 
At 8:12 PM, Anonymous Anonymous said...

I get an error

Unhandled Exception: Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments,
atureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments,
ture sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr,
er binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr,
er binder, Object[] parameters, CultureInfo culture)
at AnAppADay.CommandLineCSharp.ConsoleApp.Program.Main(String[] args)

 
At 10:03 PM, Anonymous Anonymous said...

Monad from Wikipedia

This kind of thing has been planned as the replacement for WSH in Windows for a while. Have a look. It is actually quite handy. This will be a good replacement for vbscript for performing repetitious tasks.

Cheers!

 
At 2:13 PM, Anonymous Anonymous said...

Actually, it is in the RC2 stage and has been renamed to Powershell. . It's quite a bit more complex and extremely powerful. It has some extremley cool features.

 
At 9:40 PM, Anonymous Anonymous said...

Cool another MAC app clone.. Check out Automator. And Automator.US to see all the things Automator can do.

 
At 5:07 PM, Blogger Mistadobalina said...

You might want to check out Code Runner .NET at http://www.codeplex.com/CodeRunner if you want to get really serious about using C# and .NET for scripts.

 

Post a Comment

<< Home