-2

I'm trying to redirect the standard input of a process in C#, but I'm encountering an error. Here's my code:

using System;
using System.Diagnostics;
using System.IO;

class Program
{
    static void Main()
    {
        string executablePath = "path/to/dist.exe";
        string file = "path/to/video.mp4";
        string fileName = Path.GetFileName(file);

        ExecuteExecutable(executablePath, file, fileName);
    }

    static void ExecuteExecutable(string executablePath, string file, string fileName)
    {
        ProcessStartInfo psi = new ProcessStartInfo
        {
            FileName = executablePath,
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        };

        Process process = new Process();
        process.StartInfo = psi;
        process.Start();

        process.StandardInput.WriteLine(file);
        process.StandardInput.WriteLine(fileName);

        process.WaitForExit();

        string output = process.StandardOutput.ReadToEnd();
        Console.WriteLine(output);
    }
}

I'm receiving an error when trying to write to the StandardInput stream of the process. The error message I'm getting is [: 'StandardIn has not been redirected.' ].

I've already set RedirectStandardInput to true and UseShellExecute to false in the ProcessStartInfo settings, but the error still persists. Any help or guidance on resolving this issue would be greatly appreciated.

Thank you in advance for your assistance!

Frost Dream
  • 1
  • 2
  • 13

1 Answers1

0

You just need to add this line to your code:

pri.StartInfo.RedirectStandardInput = true;
Frost Dream
  • 1
  • 2
  • 13