I'm sure this must be something really easy, but I can't seem to make it work. Let's say I have an .fsx
script file and want to cause it to be executed programmatically. I'm guessing someone must have blogged about this at some point, but I can't seem to find an example that performs my simple scenario. Basically, I want to programmatically duplicate what happens when you right click on an .fsx
file and choose "Run with F# Interactive..."
Asked
Active
Viewed 2,324 times
6

Keith Pinson
- 7,835
- 7
- 61
- 104

Beaker
- 2,804
- 5
- 33
- 54
-
There is a discussion here on embedding the fsi interpreter: http://stackoverflow.com/questions/1563024/embedding-f-interactive – sukru Apr 13 '11 at 00:27
-
That is an interesting article, but serious overkill for the scenario I am trying to implement. Thanks for mentioning it though. – Beaker Apr 13 '11 at 00:32
2 Answers
2
As asked in a comment, you can set UseShellExecute
to false to avoid opening the Windows shell. This way, you can have your output directly in F# shell:
open System.Diagnostics
let execScript script =
let psi = new ProcessStartInfo(@"c:\Program Files\Microsoft F#\v4.0\Fsi.exe")
psi.Arguments <- script
psi.UseShellExecute <- false
let p = Process.Start(psi)
p.WaitForExit()
p.ExitCode

Laurent
- 2,951
- 16
- 19
-
Thanks, that does fix an annoying issue in the code snippet I posted. :) – Beaker Apr 13 '11 at 19:58
1
After randomly messing around with the command line args I finally got it to work. I feel a little lame answering my own question, but hopefully it will still help someone else. It turned out my confusion was the proper usage of the command line arguments. If someone has something more elegant or generally useful than what I put I'll award the answer to you.
open System.Diagnostics
let launchExecutable() =
let proc = new Process()
proc.StartInfo.FileName <- @"C:\Program Files (x86)\Microsoft F#\v4.0\fsi.exe"
proc.StartInfo.Arguments <- @"--exec --nologo pathToFSXFile\Test.fsx"
proc.Start()
launchExecutable();;

Beaker
- 2,804
- 5
- 33
- 54
-
I just realized I wrote launchExecutable in camel-case. Clearly, the functional programming style is turning me to the dark side of the force. ;) – Beaker Apr 13 '11 at 14:49