C# 调起外部程序并等待该程序结束后继续运行

/// <summary>
/// 执行脚本
/// </summary>
/// <param name="str"></param>
/// <param name="startFlag"></param>
public static void runScript(string fileName, string arguments)
{
    Process p = new Process();
    try
    {
        //设置要启动的应用程序
        p.StartInfo.FileName = fileName;

        p.StartInfo.Arguments = arguments;
        //是否使用操作系统shell启动
        p.StartInfo.UseShellExecute = false;
        // 接受来自调用程序的输入信息
        p.StartInfo.RedirectStandardInput = false;
        //输出信息
        p.StartInfo.RedirectStandardOutput = false;
        // 输出错误
        p.StartInfo.RedirectStandardError = true;
        //不显示程序窗口
        p.StartInfo.CreateNoWindow = false;
        //启动程序
        p.Start();
        //向cmd窗口发送输入信息
        //p.StandardInput.WriteLine(str.ToString());
        //p.StandardInput.WriteLine("\n");
        //p.StandardInput.WriteLine("exit\n");
        p.WaitForExit();
    }
    finally
    {
        p.Close();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35106907/article/details/86568523