没有运行重定向的批处理文件输出过程 [英] Redirected batch file process output not running

查看:116
本文介绍了没有运行重定向的批处理文件输出过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个批处理文件执行输出重定向到我们的控制台应用程序的主窗口。

I'm attempting to redirect output from a batch file execution to the main window of our console application.

我打电话的方法来运行这样的过程:

I'm calling the method to run the process like this:

this.runProcess("\\bar\foo\blah\", "myBatch1.bat", "bat");

被调用的方法如下:

The method being called is as follows:

public void runProcess(string aPath,string aName,string aFiletype)
{

  Console.WriteLine();
  Console.WriteLine();
  Console.WriteLine("Started: {0}",DateTime.Now.ToString("dd-MMM hh:mm:ss"));
  Console.WriteLine("Will try run this file {0} {1}",aPath,aName);
  Console.WriteLine("File type {0}",aFiletype);

  string stInfoFileName;
  string stInfoArgs;

  if(aFiletype == "bat")
  {
    stInfoFileName = @"cmd.exe";
    stInfoArgs = "//c " + aName;
  }
  else
  { //vbs
    stInfoFileName = @"cscript";
    stInfoArgs = "//B " + aName;
  }

  this.aProcess.StartInfo.FileName = stInfoFileName;
  this.aProcess.StartInfo.Arguments =  stInfoArgs;
  this.aProcess.StartInfo.WorkingDirectory = @aPath;
  this.aProcess.StartInfo.CreateNoWindow = true;
  this.aProcess.StartInfo.UseShellExecute = false;
  this.aProcess.StartInfo.RedirectStandardError = true;
  this.aProcess.StartInfo.RedirectStandardOutput = true;

  this.aProcess.Start();
  Console.WriteLine("<<<got to here");

  Console.WriteLine(this.aProcess.StandardOutput.ReadToEnd());
  Console.WriteLine(this.aProcess.StandardError.ReadToEnd());

  this.aProcess.WaitForExit(); //<-- Optional if you want program running until your script exit
  this.aProcess.Close();

  Console.WriteLine("Finished: {0}",DateTime.Now.ToString("dd-MMM hh:mm:ss"));
}

要揣摩发生了什么我已经添加了额外的呼叫的WriteLine 。结果
&LT;&LT;&LT;到了这里被写入到控制台,然后它只是挂起并没有什么进一步的情况。

To try to figure out what is happening I've added extra calls to WriteLine.
"<<<got to here" gets written to the console then it just hangs and nothing further happens.

怀疑我的错误的东西作为我这一技术的经验很琐碎是有限的。

Suspect my mistake is something very trivial as my experience with this technology is limited.

我在做什么错了?

推荐答案

既然你想要孩子的在现有的控制台输出,你不需要任何重定向。只需设置 UseShellExecute 为false,并且没有设置 CreateNoWindow

Since you want the child's output in the existing console, you don't need any redirection. Just set UseShellExecute to false and don't set CreateNoWindow.

这code为我工作:

using System;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        Process aProcess = new Process();

        public void runProcess(string aPath, string aName, string aFiletype)
        {
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Started: {0}", DateTime.Now.ToString("dd-MMM hh:mm:ss"));
            Console.WriteLine("Will try run this file {0} {1}", aPath, aName);
            Console.WriteLine("File type {0}", aFiletype);

            string stInfoFileName;
            string stInfoArgs;

            if (aFiletype == "bat")
            {
                stInfoFileName = "cmd.exe";
                stInfoArgs = "/c " + aPath + aName;
            }
            else
            { //vbs
                stInfoFileName = "cscript";
                stInfoArgs = "/B " + aPath + aName;
            }

            this.aProcess.StartInfo.FileName = stInfoFileName;
            this.aProcess.StartInfo.Arguments = stInfoArgs;
            this.aProcess.StartInfo.WorkingDirectory = aPath;
            this.aProcess.StartInfo.UseShellExecute = false;

            this.aProcess.Start();

            Console.WriteLine("<<<got to here");

            this.aProcess.WaitForExit(); //<-- Optional if you want program running until your script exit
            this.aProcess.Close();

            Console.WriteLine("Finished: {0}", DateTime.Now.ToString("dd-MMM hh:mm:ss"));
        }

        static void Main(string[] args)
        {
            new Program().runProcess("c:\\working\\", "test.bat", "bat");
            Console.WriteLine("Exiting");
        }
    }
}

我拿出重定向和相关逻辑,并设置 CreateNoWindow 行。我还添加了 aPath 到命令行,以便它的UNC路径(不带驱动器盘符路径),因为它们不能被设置为工作目录工作。

I took out the redirection and associated logic, and the line that set CreateNoWindow. I also added aPath to the command line so that it would work for UNC paths (paths with no drive letter) since they can't be set as the working directory.

这篇关于没有运行重定向的批处理文件输出过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆