Click here to Skip to main content
15,388,688 members
Articles / Programming Languages / C#
Posted 17 Nov 2002


143 bookmarked

NScript - A script host for C#/VB.NET/JScript.NET

Rate me:
Please Sign up or sign in to vote.
4.86/5 (41 votes)
18 Nov 2002CPOL4 min read
NScript is a tool like WScript except that it allows for scripts to be written in .NET languages like C# and VB.NET


NScript is a tool similar to WScript except that it allows scripts to be written in .NET languages such as C#, VB.NET and JScript.NET. NScript automatically compiles the code into an assembly in memory and executes the assembly. The NScript setup application associates NScript with ".ncs" (for C# scripts), ".nvb" (for VB.NET scripts) and ".njs" (for JScript .NET scripts) file extensions. This enables any code written in these files to be executed directly by double clicking on these files in windows explorer. I wrote this tool when I needed to write a script for automating builds. A simple batch file was not sufficient for the task and I preferred to write code in C# as opposed to VBScript or JScript. This tool came in handy as I could modify the scripts easily and execute them by double clicking on the files in windows explorer.

Using NScript

  1. Download and unzip the setup
  2. Run the setup file NScript.msi. You need to have windows installer as well as the .NET framework installed on the machine before running the .msi file
  3. The setup automatically associates the file extensions ".ncs", ".nvb" and ".njs" with NScript.
  4. Write some code in C# like
    using System.Windows.Forms;
    class Test
        static void Main(string[] args)
            MessageBox.Show("Hello World!", "NScript");
  5. Save the file and give it an extension of .ncs

    You can execute the code in the file in any of the following ways:-

    1. Double click on the file in windows explorer in that case the script is launched using NScriptW
    2. At the command prompt type NScript MessageBox.ncs
    3. At the command prompt type NScriptw MessageBox.ncs
    4. You can execute code written in VB.NET or JScript.NET too. You need to save VB.NET files with the extension ".nvb" and JScript.NET files with extension ".njs"
  6. To cancel the execution when running in console mode press Ctrl+C or Ctrl+Break. When running under windows mode (i.e. NScriptW) an animating icon is shown in system tray. Double clicking on the icon cancels the execution.

The requirements for the code to be executed by NScript are:-

  1. The code should have a class defined.
  2. The class should have the Main method which takes single argument of type array of strings.
  3. You can refer to any assemblies listed in a file named NScript.nrf if it exists in the same directory as the NScript.Exe. Here is a sample content of NScript.nrf file
    It just has list of assembly names on each line.

How it Works?

The NScript solution has three projects:-

  1. NScript - a C# console application
  2. NScriptW - a C# windows application
  3. NScriptLib - a C# class library

NScript and NScriptW are very much similar to each other; the former can be used to run scripts that can output to console. NScript shows error messages in console where as NSCriptW shows error messages using message boxes. It is NScriptW which is associated with file extensions. Since there is lot of code that is shared by the two executables the common code is compiled in NScriptLib and both the executables refer to this class library.

The code behind NScript is pretty simple :-

  1. NScript creates an asynchronous delegate that does the compilation and execution asynchronously.
    CompileAndExecuteRoutine asyncDelegate = new 
    IAsyncResult result = asyncDelegate.BeginInvoke(fileName, 
        newargs, this, null, null);
    //For a windows app a message loop and for a 
    // console app a simple wait
  2. The compilation and execution routine creates a separate AppDomain where it does the main compilation and execution. This is done because the user may require to cancel the execution of he script, in that case the AppDomain can simply be unloaded.
    //Create an AppDomain to compile and execute the code
    //This enables us to cancel the execution if needed
    executionDomain = AppDomain.CreateDomain("ExecutionDomain");
    IScriptManager manager = (IScriptManager)
    manager.CompileAndExecuteFile(file, args, this);

    IScriptManager interface is implemented by the type ScriptManager. Since any object has to be marshaled in order for it to be referenced from a separate AppDomain, we need the interface IScriptManager (as opposed to directly using the ScriptManager object). The ScriptManager type extends the MarshalByRefObject as it is marshaled by reference.

    public class ScriptManager : MarshalByRefObject, IScriptManager
  3. The main compilation and execution is carried out by the ScriptManager object's CompileAndExecute method. It used CodeDOM to carry out the compilation. It first figures out the CodeDomProvider to use based on the extension of the input script file
    //Currently only csharp scripting is supported
    CodeDomProvider provider;
    string extension = Path.GetExtension(file);
    case ".cs":
    case ".ncs":
        provider = new Microsoft.CSharp.CSharpCodeProvider();
    case ".vb":
    case ".nvb":
        provider = new Microsoft.VisualBasic.VBCodeProvider();
    case ".njs":
    case ".js":
        provider = (CodeDomProvider)Activator.CreateInstance(
        throw new UnsupportedLanguageExecption(extension);
  4. Once we have a CodeDomProvider we can compile the file into a temporary assembly using the ICodeCompiler interface
    System.CodeDom.Compiler.ICodeCompiler compiler = 
    System.CodeDom.Compiler.CompilerParameters compilerparams = 
        new System.CodeDom.Compiler.CompilerParameters();
    compilerparams.GenerateInMemory = true;
    compilerparams.GenerateExecutable = true;
    System.CodeDom.Compiler.CompilerResults results = 
        compiler.CompileAssemblyFromFile(compilerparams, file);
  5. And finally the entry point method of the just compiled assembly is invoked.
        null, BindingFlags.Static, null, new object[]{args}, null);


This is a very simple tool and I must confess that this is in no way an original idea. Don Box wrote a similar tool long time back but I could not locate it. As a result I decided to write my own. In future I hope to enhance it by allowing to compile and execute XML files similar to ".wsh" files. As usual any suggestions are welcome.


  • November 18, 2002 - Initial posting


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rama Krishna Vavilala
United States United States
No Biography provided

Comments and Discussions

QuestionDotNetScript40 - Fork of this project compiled under .Net 4.0 Pin
Richard Schoen9-Oct-17 3:30
MemberRichard Schoen9-Oct-17 3:30 
GeneralAfter compile projecrt source: namespace name 'Windows' does not exist in the namespace 'System' Pin
ElGuroProgramador14-Oct-08 4:43
MemberElGuroProgramador14-Oct-08 4:43 
GeneralWow... what a coincidence Pin
Saul Johnson28-Jun-08 10:19
MemberSaul Johnson28-Jun-08 10:19 
GeneralRe: Wow... what a coincidence Pin
TheGreatAndPowerfulOz17-Sep-15 16:38
MemberTheGreatAndPowerfulOz17-Sep-15 16:38 
GeneralWSH.NET 2.0 (based on NScript by Rama K. Vavilala) now available as open-source Pin
bleekay13-May-08 10:34
Memberbleekay13-May-08 10:34 
QuestionCan we have objects as return value from ncs files Pin
sikunj patel21-Oct-07 23:05
Membersikunj patel21-Oct-07 23:05 
AnswerRe: Can we have objects as return value from ncs files Pin
bleekay8-Nov-07 7:24
Memberbleekay8-Nov-07 7:24 
AnswerRe: Can we have objects as return value from ncs files Pin
bleekay8-Nov-07 7:26
Memberbleekay8-Nov-07 7:26 
GeneralJ# support... Pin
bleekay5-Oct-07 9:44
Memberbleekay5-Oct-07 9:44 
GeneralRe: J# support... Pin
bleekay5-Oct-07 10:20
Memberbleekay5-Oct-07 10:20 
QuestionCan we execute C/C++ File like the .ncs files ? Pin
sikunj patel2-Oct-07 20:53
Membersikunj patel2-Oct-07 20:53 
AnswerRe: Can we execute C/C++ File like the .ncs files ? Pin
bleekay4-Oct-07 9:44
Memberbleekay4-Oct-07 9:44 
GeneralJ# support and XML script... Pin
bleekay28-Mar-07 6:50
Memberbleekay28-Mar-07 6:50 
GeneralRe: J# support and XML script... Pin
bleekay28-Mar-07 7:40
Memberbleekay28-Mar-07 7:40 
GeneralRe: J# support and XML script... Pin
bleekay29-Mar-07 5:48
Memberbleekay29-Mar-07 5:48 
GeneralRe: J# support and XML script... Pin
bleekay14-Jun-07 4:29
Memberbleekay14-Jun-07 4:29 
QuestionIntellisense? Pin
mcintyre23124-Sep-06 23:31
Membermcintyre23124-Sep-06 23:31 
AnswerRe: Intellisense? Pin
hillcstephen11-Oct-06 5:17
Memberhillcstephen11-Oct-06 5:17 
Generaladded stacktrace Pin
yeeting31-Aug-06 0:35
Memberyeeting31-Aug-06 0:35 
QuestionJIT on run-time exceptions ? Pin
ciroE26-Apr-06 7:06
MemberciroE26-Apr-06 7:06 
Generalrun js file in application Pin
hamid_m23-Apr-06 8:05
Memberhamid_m23-Apr-06 8:05 
QuestionJavascript entry point? Pin
Rei Miyasaka10-Oct-05 10:07
MemberRei Miyasaka10-Oct-05 10:07 
AnswerNevermind Pin
Rei Miyasaka10-Oct-05 10:14
MemberRei Miyasaka10-Oct-05 10:14 
Generalsame example in VB.NET for a newbie Pin
nevansmu7-Sep-05 5:12
Membernevansmu7-Sep-05 5:12 
GeneralRe: same example in VB.NET for a newbie Pin
DaveRRR11-Nov-05 0:04
MemberDaveRRR11-Nov-05 0:04 

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.