在 unity c# 中使用重定向
Using redirection in unity c#
我正在尝试使用 < 重定向来自文件的输入,它从文件 (input.txt) 发送数据并将其提供给程序,这是我 运行在终端
./rubik3Sticker.ida2 corner.bin edge1.bin edge2.bin < input.txt
我的问题是如何统一执行重定向操作?这是我的代码,它实际上是 运行 进程并接受参数,但是当涉及到重定向时,它失败了:\
void Start(){
try {
myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.FileName = "C:\Users\sam\Desktop\Test\rubik3Sticker.ida2";
myProcess.EnableRaisingEvents = true;
myProcess.StartInfo.WorkingDirectory = "C:\Users\sam\Desktop\Test";
myProcess.StartInfo.Arguments = "corner.bin edge1.bin edge2.bin";
myProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
print(e.Data);
print ("here");
}
});
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
String inputText = File.ReadAllText(inputFilePath);
myStreamWriter.Write(inputText);
myProcess.BeginOutputReadLine();
} catch (Exception e){
print(e);
}
myProcess.WaitForExit();
}
感谢任何形式的帮助,谢谢!
您不能使用 参数 进行标准输入重定向。在命令提示符中,< 重定向运算符实际上 分隔 参数形成标准输入。
以下是使用标准输入的方法:
...
myProcess.StartInfo.RedirectStandardInput = true;
...
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
String inputText = File.ReadAllText(inputFilePath);
myStreamWriter.Write(inputText);
我正在尝试使用 < 重定向来自文件的输入,它从文件 (input.txt) 发送数据并将其提供给程序,这是我 运行在终端
./rubik3Sticker.ida2 corner.bin edge1.bin edge2.bin < input.txt
我的问题是如何统一执行重定向操作?这是我的代码,它实际上是 运行 进程并接受参数,但是当涉及到重定向时,它失败了:\
void Start(){
try {
myProcess = new Process();
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.FileName = "C:\Users\sam\Desktop\Test\rubik3Sticker.ida2";
myProcess.EnableRaisingEvents = true;
myProcess.StartInfo.WorkingDirectory = "C:\Users\sam\Desktop\Test";
myProcess.StartInfo.Arguments = "corner.bin edge1.bin edge2.bin";
myProcess.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if (!String.IsNullOrEmpty(e.Data))
{
print(e.Data);
print ("here");
}
});
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
String inputText = File.ReadAllText(inputFilePath);
myStreamWriter.Write(inputText);
myProcess.BeginOutputReadLine();
} catch (Exception e){
print(e);
}
myProcess.WaitForExit();
}
感谢任何形式的帮助,谢谢!
您不能使用 参数 进行标准输入重定向。在命令提示符中,< 重定向运算符实际上 分隔 参数形成标准输入。
以下是使用标准输入的方法:
...
myProcess.StartInfo.RedirectStandardInput = true;
...
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
String inputText = File.ReadAllText(inputFilePath);
myStreamWriter.Write(inputText);