Passing arguments one by one one in console exe by c# code

后端 未结 3 1653
醉酒成梦
醉酒成梦 2020-12-21 09:04

I want to run an exe file by my c# code. The exe file is a console application written in c#.

The console application performs some actions which includes writing co

相关标签:
3条回答
  • 2020-12-21 09:22

    Here is a sample of how to pass arguments to the *.exe file:

                    Process p = new Process();
    
                    // Redirect the error stream of the child process.
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.FileName = @"\filepath.exe";
                    p.StartInfo.Arguments = "{insert arguments here}";
    
                    p.Start();
    
    
                    error += (p.StandardError.ReadToEnd());
                    p.WaitForExit();
    
    0 讨论(0)
  • 2020-12-21 09:28

    ProcessStartInfo has a constructor that you can pass arguments to:

    public ProcessStartInfo(string fileName, string arguments);
    

    Alternatively, you can set it on it's property:

    ProcessStartInfo p = new ProcessStartInfo();
    p.Arguments = "some argument";
    
    0 讨论(0)
  • 2020-12-21 09:41

    You can redirect input and output streams from your exe file.
    See redirectstandardoutput and redirectstandardinput for examples.

    For reading:

     // Start the child process.
     Process p = new Process();
     // Redirect the output stream of the child process.
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.FileName = "Write500Lines.exe";
     p.Start();
     // Do not wait for the child process to exit before
     // reading to the end of its redirected stream.
     // p.WaitForExit();
     // Read the output stream first and then wait.
     string output = p.StandardOutput.ReadToEnd();
     p.WaitForExit();
    

    For writing:

     ...
     myProcess.StartInfo.RedirectStandardInput = true;
     myProcess.Start();
    
     StreamWriter myStreamWriter = myProcess.StandardInput;
     myStreamWriter.WriteLine("y");
     ...
     myStreamWriter.Close();
    
    0 讨论(0)
提交回复
热议问题