getting result from the cmd prompt
-
Hi, I am running a DOS command from my C# application using Process.Start(cmd, arg) Does it a way to get the result of this command like "Timeout occurred", "File not found" etc... Thanks.
If u just need to get an result from a Commnad (ie dir ) then use ">" so the output wil be directed to text file c:\ dir > c:\vikas.txt A tool clip.exe from microsoft does the same and puts the data on the Windows clipboard. you can also redirect your otput to screeen stream = freopen("file.txt", "w", stdout) http://support.microsoft.com/kb/58667[^]
Vikas Amin UNITED STATES STEEL CORPORATION
My First Article on CP" Virtual Serial Port "[^]
modified on Monday, August 4, 2008 2:48 PM
-
Hi, I am running a DOS command from my C# application using Process.Start(cmd, arg) Does it a way to get the result of this command like "Timeout occurred", "File not found" etc... Thanks.
you can do it like that: Process p = new Process(); //Fill the StartInfo of the Process----"cmd.exe" p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true;//important p.StartInfo.RedirectStandardOutput = true;//important p.StartInfo.RedirectStandardError = false;//true,the same... p.StartInfo.CreateNoWindow = true;//important string strResult; p.Start();//start the process //for example p.StandardInput.WriteLine("ping www.google.com "); p.StandardInput.WriteLine("exit");//important strResult= p.StandardOutput.ReadToEnd(); // below...you want if (strResult.IndexOf("( 0% loss )") != -1) { } else if (strResult.IndexOf("Destination host unreachable.") != -1) { } else if (strResult.IndexOf("Request timed out.") != -1) { } else if (strResult.IndexOf("Unknown host") != -1) { } else { } // if end p.Close();//close the process
-
you can do it like that: Process p = new Process(); //Fill the StartInfo of the Process----"cmd.exe" p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true;//important p.StartInfo.RedirectStandardOutput = true;//important p.StartInfo.RedirectStandardError = false;//true,the same... p.StartInfo.CreateNoWindow = true;//important string strResult; p.Start();//start the process //for example p.StandardInput.WriteLine("ping www.google.com "); p.StandardInput.WriteLine("exit");//important strResult= p.StandardOutput.ReadToEnd(); // below...you want if (strResult.IndexOf("( 0% loss )") != -1) { } else if (strResult.IndexOf("Destination host unreachable.") != -1) { } else if (strResult.IndexOf("Request timed out.") != -1) { } else if (strResult.IndexOf("Unknown host") != -1) { } else { } // if end p.Close();//close the process
Hi, Thanks for the answer, it's very good. But I have a problem, the command that I am running is "tftp.exe..." when the transfer is successful I got the whole answer using the ReadToEnd function but when there is no tftp or the file doesn't exist I get only my command that I have sent I would expect to see "Timeout" or "File not found" as I see when I run the same command on the cmd prompt. Any Idea? Thanks.