Running Command line process

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • garykenneally@gmail.com

    #1

    Running Command line process

    Hi,

    Im trying to run a command line application from C# but i having some
    strange problems with it.

    Heres the code im using:

    System.Diagnost ics.Process process = new System.Diagnost ics.Process();

    procPath = @"c:\ae.bat" ;
    //string procArgs = " " + fileName + " " + tempFile;
    System.Diagnost ics.ProcessStar tInfo info = new
    System.Diagnost ics.ProcessStar tInfo(procPath );

    //info.Arguments = procArgs;

    info.UseShellEx ecute = false;
    info.RedirectSt andardOutput = true;
    info.RedirectSt andardInput = true;
    info.RedirectSt andardError = true;
    info.CreateNoWi ndow = false;
    info.WorkingDir ectory = @"c:\";

    process.StartIn fo = info;

    process.Start() ;
    StreamWriter sw = process.Standar dInput;
    StreamReader sr = process.Standar dOutput;
    StreamReader err = process.Standar dError;

    sw.AutoFlush = true;
    System.Threadin g.Thread.Sleep( 1000);

    sw.WriteLine("V alue1");
    sw.WriteLine("V alue2");
    sw.Close();
    //string ret = sr.ReadToEnd();
    //string error = err.ReadToEnd() ;


    if(process != null)
    {
    if(process.HasE xited == false)
    process.WaitFor Exit(3000);
    if(process.HasE xited == false)
    process.Kill();
    }

    If i run this code to start any command line application they all seem
    to work fine apart from the one i need. I need to run the application
    with 2 parameters and then pass it more values when it asks for them.
    I can see in debug view the test being returned from other
    applications but the one im testing with does not return anything to
    the debug window. If i run the bat file manually it works perfectly
    and prompts me to enter a value but in C# i dont see any messages
    coming from the application. I have tried using StreamReader on its
    standardoutput but it just hangs.

    Anyone have any idea why this would be happening?
  • christery@gmail.com

    #2
    Re: Running Command line process

    SendKeys works with VB, otherwise
    shell somtehing like "blaerg.exe < input.txt "
    and input .txt is like
    1
    2
    3

    //CY

    Comment

    • garykenneally@gmail.com

      #3
      Re: Running Command line process

      The redirection in DOS does not seem to work with this application
      either. Is it possible that some security features were used to stop
      this option ?


      On Jan 11, 3:10 pm, christ...@gmail .com wrote:
      SendKeys works with VB, otherwise
      shell somtehing like "blaerg.exe < input.txt "
      and input .txt is like
      1
      2
      3
      >
      //CY

      Comment

      • christery@gmail.com

        #4
        Re: Running Command line process

        On 11 Jan, 16:33, "garykennea...@ gmail.com" <garykennea...@ gmail.com>
        wrote:
        The redirection in DOS does not seem to work with this application
        either.  Is it possible that some security features were used to stop
        this option ?
        >
        I dunno, but figure about SendKeys... that simulates keypresses (this
        is in VB, havent done that in c#)
        redir ony works if stdin is console without any cntrol of cursor/user
        (that might "eat it up")

        For example if a menu is used and they want to ceep it clean (the
        keybord buffer) the program starts with
        "eating up" the redirected file. Not what we want now, but the
        programmer was not being thinking security,
        just did that to enshure that the program works without problems.

        Back to SendKeys in c# then *heh* it there is something like that...

        //CY

        Comment

        • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

          #5
          RE: Running Command line process

          The Process needs to start cmd.exe, and the arguments list needs to include
          the batfile.

          "garykenneally@ gmail.com" wrote:
          Hi,
          >
          Im trying to run a command line application from C# but i having some
          strange problems with it.
          >
          Heres the code im using:
          >
          System.Diagnost ics.Process process = new System.Diagnost ics.Process();
          >
          procPath = @"c:\ae.bat" ;
          //string procArgs = " " + fileName + " " + tempFile;
          System.Diagnost ics.ProcessStar tInfo info = new
          System.Diagnost ics.ProcessStar tInfo(procPath );
          >
          //info.Arguments = procArgs;
          >
          info.UseShellEx ecute = false;
          info.RedirectSt andardOutput = true;
          info.RedirectSt andardInput = true;
          info.RedirectSt andardError = true;
          info.CreateNoWi ndow = false;
          info.WorkingDir ectory = @"c:\";
          >
          process.StartIn fo = info;
          >
          process.Start() ;
          StreamWriter sw = process.Standar dInput;
          StreamReader sr = process.Standar dOutput;
          StreamReader err = process.Standar dError;
          >
          sw.AutoFlush = true;
          System.Threadin g.Thread.Sleep( 1000);
          >
          sw.WriteLine("V alue1");
          sw.WriteLine("V alue2");
          sw.Close();
          //string ret = sr.ReadToEnd();
          //string error = err.ReadToEnd() ;
          >
          >
          if(process != null)
          {
          if(process.HasE xited == false)
          process.WaitFor Exit(3000);
          if(process.HasE xited == false)
          process.Kill();
          }
          >
          If i run this code to start any command line application they all seem
          to work fine apart from the one i need. I need to run the application
          with 2 parameters and then pass it more values when it asks for them.
          I can see in debug view the test being returned from other
          applications but the one im testing with does not return anything to
          the debug window. If i run the bat file manually it works perfectly
          and prompts me to enter a value but in C# i dont see any messages
          coming from the application. I have tried using StreamReader on its
          standardoutput but it just hangs.
          >
          Anyone have any idea why this would be happening?
          >

          Comment

          Working...