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 847753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:50:12+00:00 2026-05-15T06:50:12+00:00

Could you please explain why the shell redirection doesn’t work with System.Diagnostics.Process class? I

  • 0

Could you please explain why the shell redirection doesn’t work with System.Diagnostics.Process class? I am trying to redirect the output streams to file with the following snippet:

Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.FileName = "java.exe";
p.StartInfo.Arguments = @"> c:\Temp\test.log 2>&1";
p.StartInfo.UseShellExecute = true;
p.Start();

The similar code works without problems with Python.
Reading the output streams programmatically doesn’t seem a preferable solution in my case because there will be a bunch of processes launched by my application.

  • 1 1 Answer
  • 1 View
  • 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-15T06:50:14+00:00Added an answer on May 15, 2026 at 6:50 am

    You cannot do the redirection because there is no shell directly involved. You could run a cmd.exe session, but the proper way to do it is using the RedirectStandardOutput/Error properties. There is no problem when having many processes. This is a class I use for this.

        class HandleExecutable {
            private DataReceivedEventHandler outputHandler;
    
            public DataReceivedEventHandler OutputHandler
            {
                set { outputHandler = value; }
            }
            private DataReceivedEventHandler errorHandler;
    
            public DataReceivedEventHandler ErrorHandler
            {
                set { errorHandler = value; }
            }
    
            public void callExecutable(string executable, string args)
            {
                string commandLine = executable;
                string args = args;
                ProcessStartInfo psi = new ProcessStartInfo(commandLine);
                psi.UseShellExecute = false;
                psi.LoadUserProfile = false;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                psi.WindowStyle = ProcessWindowStyle.Minimized;
                psi.CreateNoWindow = true;
                psi.Arguments = args;
                p = new Process();
                p.StartInfo = psi;
                try
                {
                    p.Start();
                    p.BeginOutputReadLine();
                    p.BeginErrorReadLine();
                    if (outputHandler != null) p.OutputDataReceived += outputHandler;
                    if (errorHandler != null) p.ErrorDataReceived += errorHandler;
                    p.WaitForExit();
                    p.Close();
                    p.Dispose();
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
            }
        }
    
        //On another class
        void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            //HANDLE STDERR
            if (e.Data != null && !e.Data.Equals(""))
            {
                 if (!e.Data.Contains("Something")) {
                 }
            }
        }
    
        void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            //HANDLE STDOUT
            if (e.Data != null && !e.Data.Equals(""))
            {
            }
        }
    
        HandleExecutable he = new HandleExecutable();
        he.OutputHandler = p_OutputDataReceived;
        he.ErrorHandler = p_ErrorDataReceived;
        he.callExecutable(@"C:\java.exe","-cp foo ClassName");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anyone please explain what does SomeClassname.class return in JAVA ?? I cant understand
could someone please explain to me the process of retrieving data from a remote
Could someone please explain the behavior of this class testCompile { /* * Sample
Wondering if someone could please explain the difference between these two queries and advise
Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
Could someone please explain this weird phenomenon: http://jsfiddle.net/sPA2V/ For some reason, the box footer
Could somebody please explain what this notation is in javascript? What is function(d) doing?
Could somebody please explain or link to a resource that will tell me why
Could someone please explain when would I want to use delegation instead of inheritance?
Could you please explain what is an Assembly in C# or .NET? Where does

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.