11

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".

ivanatpr
  • 1,862
  • 14
  • 18
  • 1
    Why don't you just create a batch file next to the CSX file with "ScriptCS [CSX File name].csx" in it?. Then you double click the batch file and the csx file runs? – Thundter Oct 03 '16 at 16:22

4 Answers4

3

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • So this actually works, but you can't just copy-paste the csi.exe srouce code since it relies on other internal classes from roslyn. You can actually do it by cloning the entire Roslyn source repository and changing the csi project's output type to "Window" and you'll get a csi.exe appears to run scripts without a Window. Seems like a bit of a hack though; ideally there'd be an officially supported way to do this. – ivanatpr Dec 01 '15 at 21:08
  • @ivanatpr: File a feature request on GitHub to make those classes public. – SLaks Dec 01 '15 at 21:13
2

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.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • 1
    You can also simply run the script directly; copy http://source.roslyn.io/#csi/Csi.cs,16 – SLaks Dec 01 '15 at 02:46
2

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

Alex
  • 197
  • 1
  • 4
  • 13
0

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.

bytedev
  • 8,252
  • 4
  • 48
  • 56