Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » C#, run cmd, but stream the output, and not wait until its done running?


C#, run cmd, but stream the output, and not wait until its done running?

Reply
  #1 (permalink)  
Old 12-02-08, 01:51 PM
axilant axilant is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
C#, run cmd, but stream the output, and not wait until its done running?

Code:

Code:
namespace Taynia_Updater
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo("cmd", "/c php taynia.php");
            // The following commands are needed to redirect the standard output. This means that it will be redirected to the Process.StandardOutput StreamReader.
            sinf.RedirectStandardOutput = true;
            sinf.UseShellExecute = false;
            // Do not create that ugly black window, please...
            sinf.CreateNoWindow = true;
            // Now we create a process, assign its ProcessStartInfo and start it
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = sinf;
            p.Start(); // well, we should check the return value here...
            // We can now capture the output into a string...
            string res = p.StandardOutput.ReadToEnd();
            // And do whatever we want with that.
            Console.WriteLine(res);
            Console.ReadLine();
        }
    }
}
What i'd like to do, but it won't work... compile errors, saying it wont convert from int to a string.

Code:
namespace Taynia_Updater
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo("cmd", "/c php taynia.php");
            // The following commands are needed to redirect the standard output. This means that it will be redirected to the Process.StandardOutput StreamReader.
            sinf.RedirectStandardOutput = true;
            sinf.UseShellExecute = false;
            // Do not create that ugly black window, please...
            sinf.CreateNoWindow = true;
            // Now we create a process, assign its ProcessStartInfo and start it
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = sinf;
            p.Start(); // well, we should check the return value here...
            while (Res = p.StandardOutput.Read())
            {
                Console.WriteLine(res);
            }
            // We can now capture the output into a string...
            /*string res = p.StandardOutput.ReadToEnd();
            // And do whatever we want with that.
            Console.WriteLine(res);
            
             */
            Console.ReadLine();
        }
    }
}
I'm not too familiar with C#, still learning. But this is something, I really would like to be able to do.

I don't want it to wait until it is done, i want it to stream the output, verbose mode or something along those lines. I mostly script php.
Reply With Quote
  #2 (permalink)  
Old 12-03-08, 03:28 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
trye a streamreader:

Code:
p.Start(); // well, we should check the return value here...
StreamReader myStreamReader = p.StandardOutput;
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
// We can now capture the output into a string...
Hope this helps. I assume this is where your error was.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #3 (permalink)  
Old 12-03-08, 12:07 PM
axilant axilant is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Yeroon View Post
trye a streamreader:

Code:
p.Start(); // well, we should check the return value here...
StreamReader myStreamReader = p.StandardOutput;
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
// We can now capture the output into a string...
Hope this helps. I assume this is where your error was.
Thanks bro, got me in the right direction, much appreciated, now to add error handling.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 02:17 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.