Results 1 to 3 of 3

Thread: [2.0] Redirecting Output - What am I doing wrong?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    2

    Question [2.0] Redirecting Output - What am I doing wrong?

    I'm at the end of my rope here. I've googled for weeks and looked at samples on places like this, and I'm doing everything those examples are, so why isn't it working? (I searched these forums and found nothing helpful on this subject.)

    I'm using:
    Visual Studio 2005 (8.0)
    .NET 2.0
    C#

    The sample program I'm trying to run as a process is written in C:

    Code:
    #include <stdio.h>
    main()
    {
        printf ("This message was sent to stdout.\n");
        fprintf (stderr, "This message was sent to stderr.\n");
    }
    When I run test2.exe on the command line, it outputs those two messages to the console.
    I've tried the different methods (async and sync), but this is what I'm trying (and most sources say to do):

    Code:
    private string RunTest()
    {
         string output = string.Empty;
         Process test= new Process();
         test.StartInfo.UseShellExecute = false;
         test.StartInfo.RedirectStandardOutput = true;
         test.StartInfo.CreateNoWindow = true;
         test.StartInfo.FileName = @"C:\temp\djgpp\test2.exe";
         test.Start();
         StreamReader srOutput = test.StandardOutput;
         output = srOutput.ReadToEnd();
         test.WaitForExit();
         test.Close();
         return output;
    }
    Like I've said, I've tried many different configurations, waiting before reading, using a while to read the output line by line, the event throwing BeginOutputRead things... Nothing's worked. Output is always empty. Can anyone help me?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Redirecting Output - What am I doing wrong?

    The same code worked fine for me!!
    But I got the same error you were talking about before I set the File path correctly
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    2

    Re: [2.0] Redirecting Output - What am I doing wrong?

    I may have gotten a new clue.

    I continued on with the project since getting the output was an important but not critical issue. I used the C program to output files, then moved the files it created to a new location. I noticed that the move was failing; several of the files remained in their default location (I used a if !File.Exists(...)). So it seemed to me that the process.WaitForExit wasn't working as I expected. I put
    Code:
    while (!test.HasExited())
         System.Threading.Thread.Sleep(100);
    in after the test.Start() and the output redirect. It seems to have worked for the file move problem, but I'm still not getting any redirected output.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width