簡體   English   中英

如何在Windows窗體應用程序中的c#中將標准輸出重定向到psexec進程中的文件

[英]How to redirect standard output to a file from psexec process in c# within a Windows Forms Application

我一直試圖通過運行psexec來獲取控制台輸出作為ac#windows表單應用程序中的進程。 我發現我可以將標准輸出(和標准錯誤)重定向到控制台應用程序中的指定文本文件,並且當使用的進程不是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 Forms應用程序中調用它,如下所示:

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配置

完成! 沒有ipconfig的其余結果。

在更大的應用程序中,我做了其他所有工作,我知道真正的工作已經完成(它在遠程機器或多個遠程機器上運行安裝程序),但我沒有從psexec獲得輸出。 Windows Forms應用程序是否缺少某些內容,以及如何使重定向與psexec一起使用?

好像你正在開始這個過程。 令我驚訝的是,你在控制台應用程序中得到了你想要的東西。 但我認為你想創建你的進程,掛鈎事件,然后開始:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM