最小化运行bat文件 [英] Running bat file minimised

查看:608
本文介绍了最小化运行bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从.NET应用程序运行.bat文件(实际上是一系列文件),并隐藏(或至少已经最小化)命令窗口 - 但它会坚持闪烁。尝试使用和不使用下面注释掉的行。



任何想法?



我是什么尝试过:



I am trying to run a .bat file (actually a series of them) from a .NET application, and hide (or at least have minimised) the command window - but it will insist on flashing up. Tried with and without the commented out lines below.

Any ideas?

What I have tried:

psInfo = New ProcessStartInfo(Path_to_bat_file)
psInfo.WorkingDirectory = Application.StartupPath & "\bats"
psInfo.WindowStyle = ProcessWindowStyle.Minimized
' psInfo.UseShellExecute = False
' psInfo.RedirectStandardInput = False
myProcess = Process.Start(psInfo)
myProcess.WaitForExit()

或者,如果您愿意:

psInfo = new ProcessStartInfo(Path_to_bat_file);
psInfo.WorkingDirectory = Application.StartupPath + "\\bats";
psInfo.WindowStyle = ProcessWindowStyle.Minimized;
// psInfo.UseShellExecute = false;
// psInfo.RedirectStandardInput = false;
myProcess = Process.Start(psInfo);
myProcess.WaitForExit();

推荐答案

RedirectStandardInput属性默认为false,因此注释或不相同。

请尝试以下操作;

The RedirectStandardInput property defaults to false, hence commented or not the same applies.
Try as follows;
// Create 2 stream readers to capture standard output & error if required
StreamReader srOut;
StreamReader srErr;
// create a new process
using(System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    // Set command to run in a hidden window
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.FileName = "Path and Name of BAT file";
    proc.Start();
    // Set output streams if needed
    srOut = proc.StandardOutput;
    srErr = proc.StandardError;
    proc.WaitForExit();
}
// read output & error stream if required
string strRead = srOut.ReadToEnd();
string strError = srErr.ReadToEnd();





注意:如果您的Bat文件可以挂起,那么您需要以编程方式终止它 - 它不会在桌面上显示



MSDN ProcessStartInfo类(System.Diagnostics) [ ^ ]



亲切的问候



NOTE: If your Bat file can hang then you will need to terminate it programmatically - it will not be visible on the Desktop

MSDN ProcessStartInfo Class (System.Diagnostics)[^]

Kind Regards


这篇关于最小化运行bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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