Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2006

Tip: Looking for answers? Try searching our database.

Here is some as-is code for helping with System.Diagnostics.Process

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hellosticky@gmail.com - 19 Sep 2006 21:45 GMT
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.standarderro
r.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;
           }
       }
   }
hellosticky@gmail.com - 19 Sep 2006 22:03 GMT
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();
}

> 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
[quoted text clipped - 140 lines]
>         }
>     }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.