使用ProcessStartInfo执行批处理文件 [英] Executing batch file with ProcessStartInfo

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

问题描述

我正在使用ProcessStartInfo类执行批处理文件.但是我不太了解如何正确使用它.当我执行批处理文件时,我们可以跟踪是否存在任何错误.在我的情况下,批处理将启动另一个进程,并且成功启动,它将继续执行.并且如果执行批处理文件时出现任何错误,它将退出.这是我正在使用的代码.

I am using ProcessStartInfo class to execute a batch file. But I am not so aware of using it properly. When I execute a batch file can we track is there any error or not. In my scenario, batch will start another process and it is successfully started it will be keep on executing. And if there is any error executing batch file it will exit. This is the code I am using.

我还必须跟踪成功的开始和失败.

I have to track both successfull start and failure as well.

internal bool ExecuteBatchFile(string fileName, int executionTime)
    {
        var exitCode = -1;
        var error = string.Empty;
        try
        {

            var hubStartInfo = new ProcessStartInfo
            {
                CreateNoWindow = false,
                FileName = fileName,
                UseShellExecute = false
            };

            var process = new Process { StartInfo = hubStartInfo };
            process.Start();
            process.WaitForExit(executionTime);

            if (process.HasExited)
            {
                exitCode = process.ExitCode;
                if (exitCode == 0)
                {
                    return true;
                }
                error = process.StandardError.ReadToEnd();
            }
            else
            {
                return true;
            }

            return false;
        }
        catch (Exception e)
        {
            return false;
        }
    }

谢谢

推荐答案

请参见此处以获取有关启动进程的信息.示例2包含有关监视外部过程的信息

See here for info on launching a process. Example 2 has info on monitoring the external process

private void simpleRun_Click(object sender, System.EventArgs e)
{
  System.Diagnostics.Process.Start(@"C:\listfiles.bat");
}

这篇关于使用ProcessStartInfo执行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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