C#进程中重定向StandardInput时出错。

huangapple go评论56阅读模式
英文:

Error when redirecting StandardInput in C# Process

问题

我正在尝试重定向C#进程的标准输入,但遇到了一个错误。这是我的代码:

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);
    }
}

在尝试写入进程的StandardInput流时,我收到了一个错误。错误消息如下:[: 'StandardIn has not been redirected.']。

我已经在ProcessStartInfo设置中将RedirectStandardInput设置为true,并将UseShellExecute设置为false,但错误仍然存在。对于解决此问题的任何帮助或指导,我将不胜感激。

非常感谢您的帮助!

英文:

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!

答案1

得分: 0

只需将此行添加到您的代码中:

pri.StartInfo.RedirectStandardInput = true;
英文:

You just need to add this line to your code:

pri.StartInfo.RedirectStandardInput = true;

huangapple
  • 本文由 发表于 2023年7月12日 23:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定