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. Problem executing Comman-line command - Need to press Ctrl+C to stop it !

Problem executing Comman-line command - Need to press Ctrl+C to stop it !

Scheduled Pinned Locked Moved C#
helpquestioncsharptutorial
3 Posts 2 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.
  • A Offline
    A Offline
    All Time Programming
    wrote on last edited by
    #1

    Hello, I want to execute a command line command from my C#. I have written code that works but am not as expected. The code is taken from "How to Execute a Command in C# ?" found on this site. and is : <code> public static int ExecuteCommand(string Command, int Timeout) { int ExitCode = -1; ProcessStartInfo ProcessInfo; Process Process; try { ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command); ProcessInfo.UseShellExecute = false; ProcessInfo.RedirectStandardOutput = true; //ProcessInfo.CreateNoWindow = false; //ProcessInfo.UseShellExecute = false; Process = Process.Start(ProcessInfo); // Get the results string result = Process.StandardOutput.ReadToEnd(); Console.WriteLine("Process Result = " + result); Process.WaitForExit(); ExitCode = Process.ExitCode; Console.WriteLine("%%%%%%%%%%% EXIT CODE = " + ExitCode); Process.Close(); } catch (Exception e) { Console.WriteLine("Error Processing ExecuteCommand : " + e.Message); } finally { //Process = null; //ProcessInfo = null; } return ExitCode; } </code> The code runs, but untill I don't press "Ctrl+C" on dos prompt I don't see any results. I also want to check a line form the console text. For eg: If I find "Connected" from the output I want to close the process and return. Then when the user asks for disconnecting want to execute another command to disconnect the running application. Why does the above code doesn't retun untill I press "Ctrl+c" on the command window (I don't want to show the command window also, right now am just showing)? Whne I press Ctrl+C I see the text on Console (output window), but want to check the text and on findling "Connected" want ot return and/or close the process. Can anyone point out where am I going wrong and how to solve the problem and work out with it. Any help is highly appreciated. Need to solve this early have already

    J 1 Reply Last reply
    0
    • A All Time Programming

      Hello, I want to execute a command line command from my C#. I have written code that works but am not as expected. The code is taken from "How to Execute a Command in C# ?" found on this site. and is : <code> public static int ExecuteCommand(string Command, int Timeout) { int ExitCode = -1; ProcessStartInfo ProcessInfo; Process Process; try { ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command); ProcessInfo.UseShellExecute = false; ProcessInfo.RedirectStandardOutput = true; //ProcessInfo.CreateNoWindow = false; //ProcessInfo.UseShellExecute = false; Process = Process.Start(ProcessInfo); // Get the results string result = Process.StandardOutput.ReadToEnd(); Console.WriteLine("Process Result = " + result); Process.WaitForExit(); ExitCode = Process.ExitCode; Console.WriteLine("%%%%%%%%%%% EXIT CODE = " + ExitCode); Process.Close(); } catch (Exception e) { Console.WriteLine("Error Processing ExecuteCommand : " + e.Message); } finally { //Process = null; //ProcessInfo = null; } return ExitCode; } </code> The code runs, but untill I don't press "Ctrl+C" on dos prompt I don't see any results. I also want to check a line form the console text. For eg: If I find "Connected" from the output I want to close the process and return. Then when the user asks for disconnecting want to execute another command to disconnect the running application. Why does the above code doesn't retun untill I press "Ctrl+c" on the command window (I don't want to show the command window also, right now am just showing)? Whne I press Ctrl+C I see the text on Console (output window), but want to check the text and on findling "Connected" want ot return and/or close the process. Can anyone point out where am I going wrong and how to solve the problem and work out with it. Any help is highly appreciated. Need to solve this early have already

      J Offline
      J Offline
      JF2015
      wrote on last edited by
      #2

      Please don't repost. You already asked the same question here: http://www.codeproject.com/Questions/152461/Problem-executing-a-command-line-command-from-Csha.aspx[^]

      A 1 Reply Last reply
      0
      • J JF2015

        Please don't repost. You already asked the same question here: http://www.codeproject.com/Questions/152461/Problem-executing-a-command-line-command-from-Csha.aspx[^]

        A Offline
        A Offline
        All Time Programming
        wrote on last edited by
        #3

        Yes Sorry. I found this after posting over there and didn't see the same post over here. So was wondering both are different and will that post be replied or not. So had posted here also. But now want to close this, as am geting resposne from theri also. Thanks & Regards,

        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