Click here to Skip to main content
13,446,191 members (34,910 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 18 Feb 2003

nsh: Tool to run .NET sourcefiles like script files

, 4 Jul 2004
Rate this:
Please Sign up or sign in to vote.
nsh scripts may be run either directly from Windows Explorer or from a cmd prompt.


.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

  1. Unzip files to some directory.
  2. Try running simple example from Windows Explorer: SimpleTest.vbs.
  3. Open cmd prompt in the directory.
  4. Try other included examples as follows:
    bin\nsh RefTest.cs
    bin\CmdLineArgsTest arg1 arg2
    cscript SimpleTest.vbs

Example Usage scenarios:

  • SourceFileName.vbs

    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.

Implementation notes

  • 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.


    // /reference:mylib1.dll,mylib2.dll,System.DirectoryServices.dll
                    using System;
                     . . .

    If mylib1.dll is found in working-dir, that dir is used for both mylib1 and mylib2; otherwise nsh-home-dir is used.

Related Articles

  • 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.

Other References


  • 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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
.NET solution architect / developer based in Chicago.

Check out the InRule .NET Business Rule Engine - 30 day trial download available:

You may also be interested in...


Comments and Discussions

GeneralAutomating the complete installation for web application in 2.0 Pin
aavijiaa14-Mar-08 0:06
memberaavijiaa14-Mar-08 0:06 
QuestionNot compiling in memory? Pin
WillemM5-Jul-04 5:54
memberWillemM5-Jul-04 5:54 
AnswerRe: Not compiling in memory? Pin
buc5-Jul-04 14:00
memberbuc5-Jul-04 14:00 
QuestionCan you make it work on Windows Me? Pin
sdai1-Apr-03 15:28
membersdai1-Apr-03 15:28 
AnswerRe: Can you make it work on Windows Me? Pin
sdai31-Jul-03 8:31
membersdai31-Jul-03 8:31 
AnswerRe: Can you make it work on Windows Me? Pin
Lennybacon28-Sep-03 2:04
memberLennybacon28-Sep-03 2:04 
GeneralSee Alintex Script Host Pin
Baddabing27-Feb-03 16:56
memberBaddabing27-Feb-03 16:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180314.2 | Last Updated 5 Jul 2004
Article Copyright 2003 by buc
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid