Take a look at the Process class in the System.Diagnostics namespace. I don't know if F# has any syntactic sugar for handling Windows processes, but the Process class (and the associated StartInfo) will do what you want.

By on 2/1/2011 9:54 AM ()

Hi soot, and thanks for the pointer. I did take
a look at the MSDN pages (e.g. on Process.StartInfo),
but there are no F# examples there, and it's a bit
overwhelming given that there are dozens of methods
and properties there and I'm not sure which (or how)
to use. Does anyone know of any examples anywhere,
either from experience or from any other sources
on the web? Again, this is something that is usually
done in one line in every other programming and/or
scripting language that I'm aware of (not to
knock F#, which I'm enjoying learning).

By on 2/1/2011 2:19 PM ()

The "Sys.command" function from the F# power pack does this, here's the code:

1
2
3
4
5
6
let command (s:string) = 
    let psi = new System.Diagnostics.ProcessStartInfo("cmd","/c "^s) 
    psi.UseShellExecute <- false;
    let p = System.Diagnostics.Process.Start(psi) 
    p.WaitForExit();
    p.ExitCode

FSharp.Core doesn't provide this wrapper.

This is using cmd.exe. You could also invoke the process directly. I believe cmd.exe is used because Sys.command takes a single string, but on Windows process invocation is most naturally performed with the strings already split into process namd and arguments.

thanks
don

By on 2/1/2011 2:52 PM ()

Thanks! What exactly is the line containing "psi.UseShellExecute"
supposed to be? (It's a bit unclear from your code, at least on
my browser). I tried "psi.UseShellExecute <- false" and that
seems to work ok (and to avoid having a DOS window flash
momentarily). Does that sound right? Thanks again for the
prompt response.

By on 2/1/2011 6:46 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper