winforms - Log batch in listbox C#? -
i working on program, can handle minecraft servers. running batch witch logs server, , want batch (called batch in code) log in listbox called lg_log.
if possible, how can that?
i programming in visual studio - windows forms in c#.
edit: code:
process batch = new process(); string pathtorunfile = @"\servers\base\start_server.bat"; string current_directory = directory.getcurrentdirectory(); string server_base = @"\servers\base"; string working_directory = current_directory + server_base; batch.startinfo.filename = current_directory + pathtorunfile; batch.startinfo.arguments = ""; batch.startinfo.workingdirectory = working_directory; batch.startinfo.useshellexecute = true; batch.start();
the process.startinfo
contains properties redirectstandardoutput
. setting flag true
, able add event handler batch.startinfo.outputdatareceived
, listen events. so:
edit: might want enable redirecting erroroutput in order receive error messages.
edit: requested, here working example. make sure test.bat
exists.
using system.diagnostics; using system.drawing; using system.windows.forms; public class program { public static void main() { var form = new form {clientsize = new size(400, 300)}; var button = new button {location = new point(0, 0), text = "start", size = new size(400, 22)}; var listbox = new listbox {location = new point(0, 22), size = new size(400, 278)}; form.controls.addrange(new control[] {button, listbox}); button.click += (sender, eventargs) => { var info = new processstartinfo("test.bat") {useshellexecute = false, redirectstandardoutput = true}; var proc = new process {startinfo = info, enableraisingevents = true}; proc.outputdatareceived += (obj, args) => { if (args.data != null) { listbox.items.add(args.data); } }; proc.start(); proc.beginoutputreadline(); }; form.showdialog(); } }
Comments
Post a Comment