Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 661919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:19:17+00:00 2026-05-13T23:19:17+00:00

I know that there’re stdout/in/err for a program and I want to redirect a

  • 0

I know that there’re stdout/in/err for a program and I want to redirect a program’s output to a file instead of the console output. And I now figure it out with the code below:

FileStream fs = File.Open(@"E:\console.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
TextWriter old = Console.Out;
Console.SetOut(sw);
Console.Write("bbb");
sw.Flush();

But this approach is to connect the program’s stdout to Console.Out and redirect the Console.Out to the file. It’s kind of like this:

Program.StdOut -> Console.Out -> File

The Console.Out here seems to be a bridge. I don’t want this bridge and I don’t want to use Console.Write() to make the output. How could I map the program’s stdout stream to the target file directly and write to the program’s stdout directly instead of Console.Write()? What I want is kind of like this:

Program.StdOut -> File

The Process.StandardOutput property only gives me a readonly StreamReader object. How to write to the Process.StandardOutput?? Or where is the program’s stdout?

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:19:17+00:00Added an answer on May 13, 2026 at 11:19 pm

    You can get a hold of the application’s stdout by calling Console.OpenStandardOutput. From there, you can do whatever you want with the stream, although you won’t be able to reassign it. If you want to do that you’ll have to P/Invoke SetStdHandle and handle the details yourself.

    EDIT: Added example code for the P/Invoke route:

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetStdHandle(int nStdHandle, IntPtr nHandle);
    
    const int STD_OUTPUT_HANDLE = -11;
    
    static void RedirectStandardOutput(FileStream file)
    {
        SetStdHandle(STD_OUTPUT_HANDLE, file.SafeFileHandle.DangerousGetHandle());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there is a configuration file called web.xml What I want to
I know that there are lots of posts about memory leaks. Now i am
I know that there is a function in Java to set Standard Output Stream
I know that there is the /proc/proc#/maps file that shows the libraries that are
I know that there are a few postings on this, but just want to
I know that there are some threads have a similar issue with this thread.
I know that there are lot's of questons on this, but all seem to
I know that there are others posts with solutions on the site but i
I know that there are currently questions on SO already pertaining to saving jQuery
I know that there are lots of examples out there on this, but they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.