Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. getting result from the cmd prompt

getting result from the cmd prompt

Scheduled Pinned Locked Moved C#
csharp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lune12
    wrote on last edited by
    #1

    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.

    V S 2 Replies Last reply
    0
    • L lune12

      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.

      V Offline
      V Offline
      vikas amin
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • L lune12

        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.

        S Offline
        S Offline
        smilethat
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • S smilethat

          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

          L Offline
          L Offline
          lune12
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups