.NET currently lacks a freely-available scripting language (like WSH VBScript/JScript). nsh allows you to compile and run a .NET source file with nearly the same convenience as running a WSH script, and better enables using .NET for scripting tasks. It's especially useful for running .NET scripts on servers that may not have VS.NET installed. An alternative to the NAnt build tool for simple ad-hoc needs.
There are other similar tools (see Related Articles below)--this one primarily differs in being implemented in VBScript/WSH, since one primary goal was to have an any-server-friendly "xcopy deploy" (no setup or registrations). It supports optional .config and .configscc files (the latter allows specifying that the latest be pulled from SourceSafe each time before compiled and run). The standard debug .pdb file is always generated, so any .NET debugger may be used if desired with the generated .exe.
Using the code
- Unzip files to some directory.
- Try running simple example from Windows Explorer: SimpleTest.vbs.
- Open cmd prompt in the directory.
- Try other included examples as follows:
bin\CmdLineArgsTest arg1 arg2
Example Usage scenarios:
Compiles and runs SourceFileName.cs (in same dir as SourceFileName.vbs). .vb and .js also supported. SourceFileName.vbs is simply a renamed copy of nsh.vbs--SourceFileName.cs (or .vb or .js). It is the only customized source file. Sort of a "code-behind" approach. Conveniently run from either Windows Explorer (which ends in a modal message box) or a command-prompt.
- cscript PostBuildEvent.vbs
Post-build event in VS.NET: compiles and runs PostBuildEvent.cs (in same dir as PostBuildEvent.vbs, the .*proj home dir), using cscript engine (all output to current cmd-window) - useful when custom .NET scripting needed beyond what is offered by command-line tools or WSH.
- cscript ..\..\Post-Build-Event.vbs
Compiles and runs Post-Build-Event.cs (in same dir as SourceFileName.vbs), using cscript engine (all output to current cmd-window). This provides a convenient way to script post-build events in C#, when additional flexibility is needed beyond what can be achieved using xcopy or other command-line utilities.
- nsh.vbs sourcefilename.cs
Compiles and runs only specified sourcefilename. Looks in current directory first, then in nsh-sourcedir.
- nsh.vbs *.cs
Compiles every *.cs source file found in current dir into one .exe, then runs it.
- nsh.vbs projDir\*.cs
Compiles every *.cs source file found in projDir into one .exe, then runs it.
- nsh.vbs * arg1 arg2
Compiles every .NET source file found in current dir into one .exe, then runs passing in cmd-line args: arg1, arg2.
- nsh.vbs **
Compiles every .NET source file found in current dir and any subdirs into one .exe, then runs it.
- Requires only .NET framework.
- Does not require any registration or setup (e.g., %PATH% or extension-program-association settings).
- May be run via either cscript or wscript. If run from wscript, a cmd-window is opened showing stdout-progress. In either case, all output to stdout is captured and displayed.
- EXE is created-in and run-from tempdir.
- .exe working dir is the start-up working dir.
- If script-dir includes "nsh.vbs", then the startup working dir is preserved. Otherwise, the working-dir is set to be the script dir.
- If there is a sourcefilename.config file found in the same dir as sourcefile.*, it is copied to tempdir\sourcefilename.exe.config before the run.
- If there is a sourcefilename.configscc file found in the same dir as sourcefile.*, it is used to pull the latest sourcefile.cs/vb/js (and sourcefile.config if it exists in scc).
- The .configscc file is copied to the target-exec-dir (to sourcefilename.exe.configscc), in a similar manner to the optional .config file.
- The sourcefile.cs/vb/js must already exist in the file system for this to work.
- The sourcefile.cs/vb/js and sourcefile.config must have R/O attrib set to be updated from SCC. If R/O attrib not set, the SCC get-latest is skipped.
- Currently only SourceSafe SCC provider is supported.
- One or more references may optionally be specified as follows:
/reference: is searched for in first comment line(s) of source file, if found it is passed to compiler. If first listed reference is not found in work-dir, then nsh-dir is tried. Any subsequent filename-only references are prefixed with the path where the first ref was found (except for System.*, Microsoft.*, ms* references, these are left as-is).
All reference DLLs and any same-dir dependencies they have are copied to the tempdir where the exe is run from.
. . .
If mylib1.dll is found in working-dir, that dir is used for both mylib1 and mylib2; otherwise nsh-home-dir is used.
- NScript - A script host for C#/VB.NET/JScript.NET by Rama Krishna. Posted in Nov 2002, this is very similar--it differs primarily in that it is implemented in .NET rather than VBScript. The main resulting functional difference is that it includes a setup program that registers special extensions .ncs and .nvb with Windows Explorer, contrasted with the nsh approach of using yourscriptname.vbs with a "codebehind" yourscriptname.cs paired with it (no extension registration required).
- Snippet Compiler by Jeff Key Convenient, taskbar-oriented snippet compiler for quick ad-hoc testing.
- C# Compiler By Konrad Rotuski. This is a WinForm UI app providing similar functionality that uses the .NET
ICodeCompiler interface rather than the csc/vbc compilers. nsh is oriented more toward cmd-line or automated use.
- Version 1.42: Optimized to recompile only if needed; same-named scripts originating in different dirs are run in their own %TEMP% area.
- Version 1.41: Uses latest installed framework and compiler, modifications to support non-NT (ME, 98).
- Version 1.4: Reliability improvements with the references copy to %TEMP%.
- Version 1.3: Added support for .config file, .configscc file (which supports get-latest from SourceSafe), generates .pdb debug file.
- Version 1.2: Renamed from nscript to nsh, added "codebehind" click-from-Explorer examples, added stdout-progress window when run from wscript.
- Version 1.1: Included examples.
- Version 1.0.