gpt4 book ai didi

c# - 从 plink 获取重定向输出

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:33 24 4
gpt4 key购买 nike

我目前正在尝试编写一个函数来运行 plink 并捕获该进程的输出。当我尝试 StandardOutput 时,我什么也没得到,而当我尝试 StandardError 时,我得到“无法从标准输入读取:句柄无效。”我只能假设这意味着我在 plink 等待输入时尝试阅读。我还通过名为 apCommands.txt 的文本文件传递我的命令。当我手动运行此命令时,它会工作并为我提供所需的输出,但我不明白为什么我不能重定向该输出以便将其存储在结果文件中。这是我现在拥有的代码。

        string pArgs = "-m \"" + ABSPATH + "\\apCommands.txt\" -ssh myHost -l user -pw password";
string output;

if (File.Exists(ABSPATH + "\\apTest.txt"))
File.Delete(ABSPATH + "\\apTest.txt");
StreamWriter apTest = new StreamWriter(ABSPATH + "\\apTest.txt");

Process runAP = new Process();
ProcessStartInfo apInfoStart = new ProcessStartInfo(ABSPATH + "\\plink.exe");
apInfoStart.Arguments = pArgs;
apInfoStart.RedirectStandardOutput = true;
apInfoStart.CreateNoWindow = true;
apInfoStart.UseShellExecute = false;
runAP.StartInfo = apInfoStart;
runAP.Start();

output = runAP.StandardOutput.ReadToEnd();
runAP.WaitForExit();
apTest.WriteLine("Standard Output");
apTest.WriteLine(output);
apTest.Close();
runAP.Close();

谢谢,

最佳答案

要解决此错误,请使用 apInfoStart.RefirectStandardInput = true;

另请参阅:https://social.msdn.microsoft.com/Forums/vstudio/en-US/697324fa-6ce6-4324-971a-61f6eec075be/redirecting-output-from-plink

你可以对 StandardOutput 和 StandardError 做同样的事情

关于c# - 从 plink 获取重定向输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com