Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Tip/Trick

Fast Scripting via C#

Rate me:
Please Sign up or sign in to vote.
4.86/5 (21 votes)
20 Jun 2018CPOL1 min read 28.4K   37   11
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:

Image 1

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

Image 2

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. Image 3

  3. You can run code in Visual Studio. Select all text and click «Execute in interactive»

    Image 4

  4. By developer command prompt:

    Image 5

    And do this command:

    csi <path_to_csx>

Result

Image 6

  • +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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiondebugging .csx? Pin
tdao23-Oct-19 12:03
tdao23-Oct-19 12:03 
Questioncurious about why you would use this rather than PowerShell Pin
BillWoodruff19-Jun-18 0:14
professionalBillWoodruff19-Jun-18 0:14 
AnswerRe: curious about why you would use this rather than PowerShell Pin
Glebka2120-Jun-18 7:07
Glebka2120-Jun-18 7:07 
GeneralMy vote of 5 Pin
Member 1180041218-Jun-18 2:51
Member 1180041218-Jun-18 2:51 
GeneralRe: My vote of 5 Pin
Glebka2118-Jun-18 6:12
Glebka2118-Jun-18 6:12 
QuestionSource Code Pin
Alen Toma17-Jun-18 6:20
Alen Toma17-Jun-18 6:20 
AnswerRe: Source Code Pin
User 1106097917-Jun-18 8:51
User 1106097917-Jun-18 8:51 
GeneralRe: Source Code Pin
Alen Toma17-Jun-18 9:12
Alen Toma17-Jun-18 9:12 
QuestionGreat! Pin
User 1106097916-Jun-18 8:21
User 1106097916-Jun-18 8:21 
AnswerRe: Great! Pin
Glebka2116-Jun-18 22:34
Glebka2116-Jun-18 22:34 
GeneralRe: Great! Pin
User 1106097917-Jun-18 4:33
User 1106097917-Jun-18 4:33 
You are very welcome. Still about half of the Pictures are not ok (they where ok in your very first draft)
It does not solve my Problem, but it answers my question


modified 19-Jan-21 21:04pm.

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.