Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 运行Bat文件时空白CMD窗口_C#_Cmd - Fatal编程技术网

C# 运行Bat文件时空白CMD窗口

C# 运行Bat文件时空白CMD窗口,c#,cmd,C#,Cmd,嘿,我处理这个问题已经有一段时间了。所以,我的程序的一部分要求我访问Adb(android开发桥),我通过cmd提示符和bat文件来实现。问题是我在运行我的程序时,当需要执行bat时,会出现一个空白的CMD窗口,bat在我关闭CMD窗口之前不会执行。你知道为什么吗 以下是我尝试过的: Process compiler = new Process(); compiler.StartInfo.FileName = "push.bat"; compiler.Star

嘿,我处理这个问题已经有一段时间了。所以,我的程序的一部分要求我访问Adb(android开发桥),我通过cmd提示符和bat文件来实现。问题是我在运行我的程序时,当需要执行bat时,会出现一个空白的CMD窗口,bat在我关闭CMD窗口之前不会执行。你知道为什么吗

以下是我尝试过的:

 Process compiler = new Process();
        compiler.StartInfo.FileName = "push.bat";

        compiler.StartInfo.UseShellExecute = false;
        compiler.StartInfo.RedirectStandardOutput = true;
        compiler.StartInfo.RedirectStandardError = true;

        compiler.Start();
        string d = compiler.StandardOutput.ReadToEnd();
        MessageBox.Show(d);
空白CMD窗口。我也试过这个

    Process compiler = new Process();
        compiler.StartInfo.FileName = "cmd.exe";
        compiler.StartInfo.Arguments = " /c push.bat";
        compiler.StartInfo.UseShellExecute = false;
        compiler.StartInfo.RedirectStandardOutput = true;
        compiler.StartInfo.RedirectStandardError = true;

        compiler.Start();
        string d = compiler.StandardOutput.ReadToEnd();
        MessageBox.Show(d);

仍然是空的CMD窗口,光标闪烁,在我关闭它之前不会做任何事情

尝试“启动/b SOMECOMMAND”调用您的命令(或者,在.bat文件中)

我认为发生的情况是,您一直在读取,直到流关闭,但直到
push.bat
退出才关闭

尝试使用and事件和
()
方法

这将允许您异步读取数据,并且当您的调用通过
WaitForExit()
调用时,您将知道数据何时退出

示例:

Process compiler = new Process();
compiler.StartInfo.FileName = "push.bat";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.StartInfo.RedirectStandardError = true;

var d = new StringBuilder();
compiler.OutputDataReceived += (o, e) => d.AppendLine(e.Data);
compiler.ErrorDataReceived += (o, e) => d.AppendLine(e.Data);
compiler.Start();
compiler.WaitForExit();
MessageBox.Show(d.ToString());

为什么将进程命名为编译器?这可能无关紧要,但它只是奇怪。这就像将文件读取器称为解释器或将BufferedReader称为JITCompiler.idk一样,只是一个随机名称。我经常这样做,但没有问题!感谢您实际接受答案,而不是放弃答案。:)