public String Compile(String src, String filename, String nxtID) { filename += ".rxe"; Guid guidFilename = Guid.NewGuid(); String srcFilename = String.Format(@"C:\inetpub\wwwroot\CompiledApps\{0}.nxc", guidFilename); String compiledFilename = String.Format(@"C:\inetpub\wwwroot\CompiledApps\{0}.rxe", guidFilename); File.WriteAllText(srcFilename, src); RunResults runResults = Program.RunExecutable(@"C:\inetpub\wwwroot\nbc.exe", "-O=" + compiledFilename + " " + srcFilename, "."); if (runResults.RunException != null) { System.Diagnostics.Trace.WriteLine(runResults.RunException); } else { System.Diagnostics.Trace.WriteLine("Output"); System.Diagnostics.Trace.WriteLine("======"); System.Diagnostics.Trace.WriteLine(runResults.Output); System.Diagnostics.Trace.WriteLine("Error"); System.Diagnostics.Trace.WriteLine("====="); System.Diagnostics.Trace.WriteLine(runResults.Error); if (runResults.Error.Length < 3) { // An empty error list is "{}" SendProgramToNXT(compiledFilename, filename, nxtID); } File.Delete(srcFilename); return(runResults.Error.ToString()); } return(null); }
public static RunResults RunExecutable(string executablePath, string arguments, string workingDirectory) { RunResults runResults = new RunResults { Output = new StringBuilder(), Error = new StringBuilder(), RunException = null }; try { if (File.Exists(executablePath)) { using (Process proc = new Process()) { proc.StartInfo.FileName = executablePath; proc.StartInfo.Arguments = arguments; proc.StartInfo.WorkingDirectory = workingDirectory; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.OutputDataReceived += (o, e) => runResults.Output.Append(e.Data).Append(Environment.NewLine); proc.ErrorDataReceived += (o, e) => runResults.Error.Append(e.Data).Append(Environment.NewLine); proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); proc.WaitForExit(); runResults.ExitCode = proc.ExitCode; } } else { throw new ArgumentException("Invalid executable path.", "exePath"); } } catch (Exception e) { runResults.RunException = e; } return(runResults); }