public CommandResult ExecuteCommand(string command, IOutputReceiver receiver = null, Type resultType = null, params string[] arguments) { if (receiver == null) { receiver = new OutputCollector(); } int exitCode = 0; string commandline = $"{command} {string.Join(" ", arguments)}"; using (ProcessFacade f = new ProcessFacade(PlcncliCommand, commandline, receiver, CancellationToken.None)) { f.WaitForExit(); exitCode = f.ExitCode; } if (exitCode != 0) { throw new PlcncliException(command, receiver.InfoMessages, receiver.ErrorMessages); } if (resultType == null) { return(null); } List <string> infos = receiver.InfoMessages; var result = JsonConvert.DeserializeObject(string.Join("", infos.SkipWhile(s => !s.Trim().StartsWith("{"))), resultType ?? typeof(CommandResult)); return(result as CommandResult); }
public void ExecuteWithoutResult(string command, IOutputReceiver receiver = null, params string[] arguments) { if (receiver == null) { receiver = new OutputCollector(); } int exitCode = 0; string commandline = $"{command} {string.Join(" ", arguments)}"; receiver.LogDebugInfo($"Starting process {PlcncliCommand} with options {commandline}"); using (ProcessFacade f = new ProcessFacade(PlcncliCommand, commandline, receiver, CancellationToken.None)) { f.WaitForExit(); exitCode = f.ExitCode; } if (exitCode != 0) { throw new PlcncliException(command, receiver.InfoMessages, receiver.ErrorMessages); } }