|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Here is some as-is code for helping with System.Diagnostics.ProcessDeals with a common use of Process which is to encapsulate another process and grab its standard out and standard error, and so it uses the asynchronous operations that the Process class provides. Sorry for lack of commenting. This code is provided As Is, with no warranty implied, and can be used for any purpose, royalty free. // Common usage: ProcessHelper realProcess = new ProcessHelper(); realProcess.FileName = @"c:\program files\subversion\bin\svn.exe"; realProcess.SetArguments("info"); realProcess.Start(true); /// <summary> /// See http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx /// </summary> public class ProcessHelper { private Process m_process = new Process(); private TextWriter m_error = Console.Error; private TextWriter m_out = Console.Out; public ProcessHelper() { m_process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived); m_process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived); } public string FileName { get { return m_process.StartInfo.FileName; } set { m_process.StartInfo.FileName = value; } } public string Arguments { get { return m_process.StartInfo.Arguments; } set { m_process.StartInfo.Arguments = value; } } public void SetArguments(params string[] args) { Arguments = string.Join(" ", args); } protected virtual void process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (e.Data != null) { m_out.WriteLine(e.Data); } } protected virtual void process_ErrorDataReceived(object sender, DataReceivedEventArgs e) { if (e.Data != null) { m_error.WriteLine(e.Data); } } public Process Process { get { return m_process; } set { m_process = value; } } public ProcessStartInfo StartInfo { get { return m_process.StartInfo; } } public void Start(bool waitForExit) { // Initialize the asynchronous stuff m_process.StartInfo.UseShellExecute = false; m_process.StartInfo.RedirectStandardError = true; m_process.StartInfo.RedirectStandardOutput = true; m_process.StartInfo.WorkingDirectory = Environment.CurrentDirectory; m_process.StartInfo.CreateNoWindow = true; m_process.Start(); m_process.BeginErrorReadLine(); m_process.BeginOutputReadLine(); if (waitForExit) { m_process.WaitForExit(); } } public TextWriter Error { get { return m_error; } set { m_error = value; } } public TextWriter Out { get { return m_out; } set { m_out = value; } } } Updated version of SetArguments method:
public void SetArguments(params string[] args) { StringBuilder sb = new StringBuilder(); int c = 0; bool containsSpace; foreach (string val in args) { if (c > 0) { sb.Append(' '); } containsSpace = (val.IndexOf(' ') != -1); if (containsSpace) { sb.Append('\"'); } sb.Append(val); if (containsSpace) { sb.Append('\"'); } c++; } Arguments = sb.ToString(); } hellosti***@gmail.com wrote: Show quote > Thought I would throw this out there so that it could be Google'd. > Deals with a common use of Process which is to encapsulate another > process and grab its standard out and standard error, and so it uses > the asynchronous operations that the Process class provides. Sorry for > lack of commenting. This code is provided As Is, with no warranty > implied, and can be used for any purpose, royalty free. > > // Common usage: > ProcessHelper realProcess = new ProcessHelper(); > realProcess.FileName = @"c:\program > files\subversion\bin\svn.exe"; > realProcess.SetArguments("info"); > realProcess.Start(true); > > > /// <summary> > /// See > http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx > /// </summary> > public class ProcessHelper > { > private Process m_process = new Process(); > private TextWriter m_error = Console.Error; > private TextWriter m_out = Console.Out; > > public ProcessHelper() > { > m_process.ErrorDataReceived += new > DataReceivedEventHandler(process_ErrorDataReceived); > m_process.OutputDataReceived += new > DataReceivedEventHandler(process_OutputDataReceived); > } > > public string FileName > { > get > { > return m_process.StartInfo.FileName; > } > set > { > m_process.StartInfo.FileName = value; > } > } > > public string Arguments > { > get > { > return m_process.StartInfo.Arguments; > } > set > { > m_process.StartInfo.Arguments = value; > } > } > > public void SetArguments(params string[] args) > { > Arguments = string.Join(" ", args); > } > > protected virtual void process_OutputDataReceived(object > sender, DataReceivedEventArgs e) > { > if (e.Data != null) > { > m_out.WriteLine(e.Data); > } > } > > protected virtual void process_ErrorDataReceived(object sender, > DataReceivedEventArgs e) > { > if (e.Data != null) > { > m_error.WriteLine(e.Data); > } > } > > public Process Process > { > get > { > return m_process; > } > set > { > m_process = value; > } > } > > public ProcessStartInfo StartInfo > { > get > { > return m_process.StartInfo; > } > } > > public void Start(bool waitForExit) > { > // Initialize the asynchronous stuff > m_process.StartInfo.UseShellExecute = false; > m_process.StartInfo.RedirectStandardError = true; > m_process.StartInfo.RedirectStandardOutput = true; > m_process.StartInfo.WorkingDirectory = > Environment.CurrentDirectory; > m_process.StartInfo.CreateNoWindow = true; > > m_process.Start(); > > m_process.BeginErrorReadLine(); > m_process.BeginOutputReadLine(); > > if (waitForExit) > { > m_process.WaitForExit(); > } > } > > public TextWriter Error > { > get > { > return m_error; > } > set > { > m_error = value; > } > } > > public TextWriter Out > { > get > { > return m_out; > } > set > { > m_out = value; > } > } > } |
|||||||||||||||||||||||