英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论