Executor.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // System.CodeDom.Compiler.Executor.cs
  3. //
  4. // Author(s):
  5. // Andreas Nahr ([email protected])
  6. //
  7. // (C) 2003 Andreas Nahr
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Diagnostics;
  12. using System.Threading;
  13. namespace System.CodeDom.Compiler
  14. {
  15. public sealed class Executor
  16. {
  17. class ProcessResultReader
  18. {
  19. StreamReader reader;
  20. string file;
  21. public ProcessResultReader (StreamReader reader, string file)
  22. {
  23. this.reader = reader;
  24. this.file = file;
  25. }
  26. public void Read ()
  27. {
  28. StreamWriter sw = new StreamWriter (file);
  29. try
  30. {
  31. string line;
  32. while ((line = reader.ReadLine()) != null)
  33. sw.WriteLine (line);
  34. }
  35. finally
  36. {
  37. sw.Close ();
  38. }
  39. }
  40. }
  41. private Executor ()
  42. {
  43. }
  44. public static void ExecWait (string cmd, TempFileCollection tempFiles)
  45. {
  46. string outputName = null;
  47. string errorName = null;
  48. ExecWaitWithCapture (IntPtr.Zero, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  49. }
  50. [MonoTODO ("Do something with userToken")]
  51. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  52. {
  53. if (outputName == null)
  54. outputName = tempFiles.AddExtension ("out");
  55. if (errorName == null)
  56. errorName = tempFiles.AddExtension ("err");
  57. Process proc = new Process();
  58. proc.StartInfo.FileName = cmd;
  59. proc.StartInfo.CreateNoWindow = true;
  60. proc.StartInfo.UseShellExecute = false;
  61. proc.StartInfo.RedirectStandardOutput = true;
  62. proc.StartInfo.RedirectStandardError = true;
  63. proc.StartInfo.WorkingDirectory = currentDir;
  64. try
  65. {
  66. proc.Start();
  67. ProcessResultReader outReader = new ProcessResultReader (proc.StandardOutput, outputName);
  68. ProcessResultReader errReader = new ProcessResultReader (proc.StandardError, errorName);
  69. Thread t = new Thread (new ThreadStart (errReader.Read));
  70. t.Start ();
  71. outReader.Read ();
  72. t.Join ();
  73. proc.WaitForExit();
  74. }
  75. finally
  76. {
  77. proc.Close();
  78. }
  79. return proc.ExitCode;
  80. }
  81. public static Int32 ExecWaitWithCapture (IntPtr userToken, string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  82. {
  83. return ExecWaitWithCapture (userToken, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  84. }
  85. public static Int32 ExecWaitWithCapture (string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName )
  86. {
  87. return ExecWaitWithCapture (IntPtr.Zero, cmd, currentDir, tempFiles, ref outputName, ref errorName);
  88. }
  89. public static Int32 ExecWaitWithCapture (string cmd, TempFileCollection tempFiles, ref string outputName, ref string errorName)
  90. {
  91. return ExecWaitWithCapture (IntPtr.Zero, cmd, Environment.CurrentDirectory, tempFiles, ref outputName, ref errorName);
  92. }
  93. }
  94. }