Executing C# Scripts from Command Line or C# Interactive Windows in Visual Studio

Executing C# Scripts from Command Line or C# Interactive Windows in Visual Studio

C# Script files is similar like other script files that contain a piece of C# code and can execute and returns the results.  As I said it is only piece of code – you really don’t have to have like a class  or method to get the result. The C# script file (CSX) file is  it’s own container and execute like a method. For Visual Studio, you can execute the CSX Script either using C# Interactive Window or CSI Command Line Window.

Before your proceed further, you must read following two posts to understand more about the new C# Interactive Window and the Command Line Interactive script engine for Visual Studio.

Consider you have following C# Script file named “MyScript.csx” which just print a series of number from a defined collection.

C# Script in VS Code
C# Script in VS Code

To run the script from the C# Interactive Window, write the following command and press enter.

#load "D:\Myscript.csx"

You will have following output :;

Execute Script from C# Interactive Window
Execute Script from C# Interactive Window

Similarly from the Command Prompt, open the Visual Studio Command Prompt and run the following command.

csi d:\MyScript.csx

Once the script loads and execute, you will have the following output.

Execute Script from Command Line Interactive Window
Execute Script from Command Line Interactive Window

Hope this helps !

DON'T MISS OUT!
Subscribe To Newsletter For your Daily Development Tips and Tricks  

 Best Tips & Tricks on Microsoft Technology Platform 

Invalid email address
We promise not to spam you. You can unsubscribe at any time.
Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

5 Comments to “Executing C# Scripts from Command Line or C# Interactive Windows in Visual Studio”

Comments are closed.