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();
}
Download the app here
Download the source here
10 Comments:
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?
how to apply a regex in one line, then?
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.
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.
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.
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)
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!
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.
Cool another MAC app clone.. Check out Automator. And Automator.US to see all the things Automator can do.
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