c# - 如何在 Windows 窗体应用程序中将标准输出重定向到 c# 中的 psexec 进程的文件

标签 c# winforms psexec redirectstandardoutput

我一直在尝试通过在 c# Windows 窗体应用程序中将 psexec 作为进程运行来获取控制台输出。我发现我可以在控制台应用程序中将标准输出(和标准错误)重定向到指定的文本文件,并且可以在使用的进程不是 PsExec(例如 ping)时重定向输出,但是当我尝试在 Windows 窗体应用程序中使用 psexec 我通常在日志中得到一个空行,或者充其量我已经能够得到第一行。我知道 psexec 在同步重定向输出方面存在问题,但即使是异步也会遇到此问题,但仅限于在 Windows 窗体应用程序中使用时。

我调用的运行进程的方法的代码:

class Tester
    {

        static readonly StringBuilder outputText = new StringBuilder();
        static readonly StringBuilder errorText = new StringBuilder();

        public void Installer(string command, string arguments)
        {
            using (var psexec = Process.Start(new ProcessStartInfo(
               command,
                arguments)
            {
                CreateNoWindow = true,
                ErrorDialog = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                UseShellExecute = false
            }))
            {

                psexec.OutputDataReceived += (sendingProcess, outLine) =>
                    outputText.AppendLine(outLine.Data);

                psexec.ErrorDataReceived += (sendingProcess, errorLine) =>
                    errorText.AppendLine(errorLine.Data);

                psexec.BeginOutputReadLine();
                psexec.BeginErrorReadLine();

                psexec.WaitForExit();

                string text = outputText.ToString();
                File.AppendAllText(@"C:\test\psexec-test.log", text);

            }

        }
    }

在这样的控制台应用程序中调用时,上面的工作(给了我期望在指定文件中的 psexec 的输出):

class Program
{
    static void Main(string[] args)
    {
        Tester test1 = new Tester();

        test1.Installer("PsExec.exe", @"-h \\remoteserver ipconfig");
    }
}

但是,如果我像这样从等效的 Windows 窗体应用程序调用它:

public partial class Form1 : Form
{

    Tester test = new Tester();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string sendAddress = Address.Text;
        test.Installer("psexec.exe", @"-h \\remoteserver ipconfig");
    } }

我只得到:

Windows IP Configuration

Done! Without the rest of the results from ipconfig.

在更大的应用程序中,我让其他一切正常工作,我知道真正的工作已经完成(它在远程机器或多台远程机器上运行安装程序),但我没有从 psexec 获得输出。 Windows 窗体应用程序是否缺少某些内容,以及如何使重定向与 psexec 一起使用?

最佳答案

似乎您在开始该过程时仓促行事。令我惊讶的是,您在控制台应用程序中得到了您想要的东西。但我认为您想创建自己的流程, Hook 事件,然后开始:

public void Installer( string command, string arguments, string logFile )
{
  var outputText = new StringBuilder( );
  var errorText = new StringBuilder( );

  var startInfo = new ProcessStartInfo( command, arguments )
  {
    CreateNoWindow = true,
    ErrorDialog = false,
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    UseShellExecute = false
  };

  using ( var psexec = new Process( ) )
  {
    psexec.StartInfo = startInfo;
    psexec.OutputDataReceived += ( _, dat ) => outputText.AppendLine( dat.Data );
    psexec.ErrorDataReceived += ( _, dat ) => errorText.AppendLine( dat.Data );
    psexec.Start( );
    psexec.BeginOutputReadLine( );
    psexec.BeginErrorReadLine( );
    psexec.WaitForExit( );

    File.AppendAllText( logFile, outputText.ToString( ) );
  }
}

关于c# - 如何在 Windows 窗体应用程序中将标准输出重定向到 c# 中的 psexec 进程的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32104958/

相关文章:

c# - 单声道项目 : Why is mono faster than . NET?

c# - 将字符串解析为 int 数组

winforms - Visual Studio Winforms Designer的隐藏功能

c# - Vlc.DotNet - 在播放音频之前无法设置音量

PowerShell 为 UIAutomation 打开远程 GUI session

c# - 如何确定退出代码来自哪里?

c# - 如何使用 zlib.NET 扩充文件?

c# - Paypal 与 ASP.NET/C# 集成

ant - 将 Ant 参数列表复制到属性中

c# - 检测 PrintDocument 何时成功打印(不仅仅是预览)