Launch a .csx (scriptcs) C# script directly without opening a command line window

4.9k Views Asked by At

Is there a way in Windows to launch a .csx script directly (e.g. by double clicking the file, or from the start menu, or via an app launcher like Launchy) such that the script runs in the background without opening a command line window?

For example, say I have a simple "lowercase.csx" script which simply takes whatever text is in my clipboard, converts it to lowercase, and puts the result back in my clipboard. I want to be able to double click on that .csx file and have that run completely in the background without opening any kind of window.

Currently both ScriptCS.exe and the new csi.exe launcher that comes with VS2015 Update 1 will open a console window when running a .csx file, even if that file doesn't output anything to the console.

Note: I know that a new window isn't opened if you launch a script from a console window. What I want to to launch a script in a non-command line context and have it open no windows.

Note2: The equivalent of what I want in "CS-Script" (a ScriptCS alternative) is to launch the files using "csws.exe".

4

There are 4 best solutions below

0
On BEST ANSWER

scriptcs does not support this. However there is an application that solves this problem: https://gitlab.com/Lions-Open-Source/ScriptCSW

2
On

You can make your own version of csi.exe that runs without a console.

Simply create a new project, make sure the Type is set to WinForms instead of Console, then add the C# Scripting package from NuGet and copy-paste the csi.exe source code.

1
On

A workaround is to create your own program that launches it in the background for you, like csws.exe would.

Start by creating a console app.

public static void Main(string[] args)
{
    var startInfo = new ProcessStartInfo("path to csi.exe")
    {
        CreateNoWindow = true,
        Arguments = args[1],
        RedirectStandardOutput = true,
        UseShellExecute = false,
    };
    Process.Start(startInfo);
}

(You'll obviously want to pass the rest of the arguments and check to make sure a file was passed etc, but this should get you started. And I wrote this answer on my Mac as my Windows box is in the shop, so I can't guarantee it'll work.)

Then have the console app not show a window by changing it to a Windows application from a Console application on the project properties screen as described in this question.

Lastly, configure .csx files to always open with your application. Then you can double click them and they'll run with no window.

0
On

Create a batch file and add (in this example to csi.exe installed for VS 2019):

@echo off
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Roslyn\csi.exe" "C:\myscript.csx"

If its important to hide the command prompt then see here and here.