Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Fast Scripting via C#

0.00/5 (No votes)
20 Jun 2018 1  
Quick and simple

Often, there is a need to quickly write a small script, with absolutely no desire to understand the syntax of bat-files or create a whole project for these cases.

There is one way to quickly write the script in C# without unnecessary movements.

First, create a new text file and change extension to *.csx:

Next, open file in your favourite code editor (I am using Visual Studio) and write the needed code:

Running

You can run this code in several ways:

  1. Simplest - open file by csi.exe (%programfiles(x86)%\MSBuild\14.0\Bin\csi.exe or %programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn):
  2. You can run code in Visual Studio. Select all text and click «Execute in interactive»

  3. By developer command prompt:

    And do this command:

    csi <path_to_csx>

Result

  • +Written in C#
  • +Fast
  • +IDE pluses (IntelliSense, etc.)
  • - Requires csi.exe (by default in .NET Framework)

Using Libraries

Simple! Write:

#r "MyLib.dll"

in the beginning of *.csx file and copy DLLs to "bin" folder (create this folder near the your script file).
Sample:

MyScript.csx
Bin/MyLib.dll

About CSI

Csi.exe is the Microsoft's command line RELP (read-evaluate-print-loop) tool.

It enables the use of C# as a scripting language by executing source code files directly. The source code IS the executable, so it is easy to make changes and there is no need to maintain a separate EXE file. CSI compiles source code "on the fly" and executes the resulting assembly seamlessly.

This tool uses Microsoft .NET Compiler Platform ("Roslyn") like a "C# Interactive" window in Visual Studio.

License

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