spawn - Running / spawning commands vs files in c# -
i can in c# following:
var pspawn = new process { startinfo = { workingdirectory = @"c:\temp", filename = filetorun, createnowindow = true } }; pspawn.start();
and works fine.... wondering if there way run command (ie: "dir /b") without having encapsulate in batch file?
just start cmd.exe , pass arguments required
var pspawn = new process { startinfo = { workingdirectory = @"c:\temp", filename = "cmd.exe", arguments ="/k dir /b" } }; pspawn.start();
i have added parameter /k
leave command window open so, possible see output of command dir
.
of course think interested catch output of command.
in case work this:
stringbuilder sb = new stringbuilder(); var pspawn = new process { startinfo = { workingdirectory = @"c:\temp", filename = "cmd.exe", arguments ="/c dir /b", createnowindow = true, redirectstandardoutput = true, redirectstandardinput = true, useshellexecute = false } }; pspawn.outputdatareceived += (sender, args) => sb.appendline(args.data); pspawn.start(); pspawn.beginoutputreadline(); pspawn.waitforexit(); console.writeline(sb.tostring());