C#执行.exe文件返回输出流(字符串)到文件

1 篇文章 0 订阅

1,一般我们做一些程序可能会调用到其他已经写好的并发布生成的.exe文件,以下就是我的调用过程代码和一些源码的解释:

/// <summary>
        /// 执行exe来编译来生成table的xml模板
        /// </summary>
        /// <param name="TableID">执行需要的参数(我这里是表的ID)</param>
        /// <param name="sFilePath">生成的xml文件存放的绝对路径和文件名</param>
        /// <param name="exeFilePath">exe执行文件路径</param>
        public static void ExecProc(string TableID, string sFilePath, string exeFilePath)
        {
            Process process1 = new Process();//创建进程对象 
            ProcessStartInfo startInfo = new ProcessStartInfo();//进程信息对象
            //bool isFileReadOnly = SetFileReadAccess(sFilePath, false);
            if (TableID != null)
            {
                startInfo.FileName = @exeFilePath;//执行exe文件
                startInfo.Arguments = TableID;//参数
                startInfo.RedirectStandardInput = false;
                startInfo.RedirectStandardOutput = true;
                startInfo.CreateNoWindow = true; //不创建窗口  
                startInfo.UseShellExecute = false;// 不使用系统外壳程序启动
                process1.StartInfo = startInfo;
            }
            try
            {
                process1.Start();
                string txt = process1.StandardOutput.ReadToEnd();//获取执行结果值
                File.WriteAllText(sFilePath, txt, Encoding.UTF8);//将结果写入文件并保存
                process1.WaitForExit();//进程等待
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.Data + DateTime.Now.ToString("yyyy/hh/dd HH/mm/ss"));//捕获异常,输出异常信息
            }
            finally
            {
                if (process1 != null)
                    process1.Close();//最后记得关闭进程
                //if (isFileReadOnly)
                //    SetFileReadAccess(sFilePath, true);
            }
        }

2,具体函数的功能解释,复制于C#源代码

//
        // 摘要:
        //     启动由包含进程启动信息(例如,要启动的进程的文件名)的参数指定的进程资源,并将该资源与新的 System.Diagnostics.Process
        //     组件关联。
        //
        // 参数:
        //   startInfo:
        //     System.Diagnostics.ProcessStartInfo,包含用于启动进程的信息(包括文件名和任何命令行参数)。
        //
        // 返回结果:
        //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
        //
        // 异常:
        //   System.InvalidOperationException:
        //     在 startInfo 参数的 System.Diagnostics.ProcessStartInfo.FileName 属性中未指定任何文件名。-
        //     或 - startInfo 参数的 System.Diagnostics.ProcessStartInfo.UseShellExecute 属性为
        //     true,而 System.Diagnostics.ProcessStartInfo.RedirectStandardInput、System.Diagnostics.ProcessStartInfo.RedirectStandardOutput
        //     或 System.Diagnostics.ProcessStartInfo.RedirectStandardError 属性也为 true。- 或
        //     -startInfo 参数的 System.Diagnostics.ProcessStartInfo.UseShellExecute 属性为 true,而
        //     System.Diagnostics.ProcessStartInfo.UserName 属性不为 null 或空,或者 System.Diagnostics.ProcessStartInfo.Password
        //     属性不为 null。
        //
        //   System.ArgumentNullException:
        //     startInfo 参数为 null。
        //
        //   System.ComponentModel.Win32Exception:
        //     打开关联的文件时发生了错误。
        //
        //   System.ObjectDisposedException:
        //     该进程对象已被释放。
        public static Process Start(ProcessStartInfo startInfo);
        //
        // 摘要:
        //     通过指定文档或应用程序文件的名称来启动进程资源,并将资源与新的 System.Diagnostics.Process 组件关联。
        //
        // 参数:
        //   fileName:
        //     要在进程中运行的文档或应用程序文件的名称。
        //
        // 返回结果:
        //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
        //
        // 异常:
        //   System.ComponentModel.Win32Exception:
        //     打开关联的文件时发生了错误。
        //
        //   System.ObjectDisposedException:
        //     该进程对象已被释放。
        //
        //   System.IO.FileNotFoundException:
        //     PATH 环境变量有包含引号的字符串。
        public static Process Start(string fileName);
        //
        // 摘要:
        //     通过指定应用程序的名称和一组命令行参数来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process 组件相关联。
        //
        // 参数:
        //   fileName:
        //     要在该进程中运行的应用程序文件的名称。
        //
        //   arguments:
        //     启动该进程时传递的命令行参数。
        //
        // 返回结果:
        //     与该进程关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
        //
        // 异常:
        //   System.InvalidOperationException:
        //     fileName 或 arguments 参数为 null。
        //
        //   System.ComponentModel.Win32Exception:
        //     打开关联的文件时发生了错误。
        //
        //   System.ObjectDisposedException:
        //     该进程对象已被释放。
        //
        //   System.IO.FileNotFoundException:
        //     PATH 环境变量有包含引号的字符串。
        public static Process Start(string fileName, string arguments);
        //
        // 摘要:
        //     通过指定应用程序的名称、用户名、密码和域来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process 组件关联起来。
        //
        // 参数:
        //   fileName:
        //     要在该进程中运行的应用程序文件的名称。
        //
        //   userName:
        //     启动进程时使用的用户名。
        //
        //   password:
        //     一个 System.Security.SecureString,它包含启动进程时要使用的密码。
        //
        //   domain:
        //     启动进程时要使用的域。
        //
        // 返回结果:
        //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
        //
        // 异常:
        //   System.InvalidOperationException:
        //     未指定文件名。
        //
        //   System.ComponentModel.Win32Exception:
        //     fileName 不是可执行 (.exe) 文件。
        //
        //   System.ComponentModel.Win32Exception:
        //     打开关联的文件时发生了错误。
        //
        //   System.ObjectDisposedException:
        //     该进程对象已被释放。
        public static Process Start(string fileName, string userName, SecureString password, string domain);
        //
        // 摘要:
        //     通过指定应用程序的名称、一组命令行参数、用户名、密码和域来启动一个进程资源,并将该资源与新的 System.Diagnostics.Process
        //     组件关联起来。
        //
        // 参数:
        //   fileName:
        //     要在该进程中运行的应用程序文件的名称。
        //
        //   arguments:
        //     启动该进程时传递的命令行参数。
        //
        //   userName:
        //     启动进程时要使用的用户名。
        //
        //   password:
        //     一个 System.Security.SecureString,它包含启动进程时要使用的密码。
        //
        //   domain:
        //     启动进程时要使用的域。
        //
        // 返回结果:
        //     与进程资源关联的新的 System.Diagnostics.Process 组件,或者如果没有启动进程资源(例如,如果重用了现有进程),则为 null。
        //
        // 异常:
        //   System.InvalidOperationException:
        //     未指定文件名。
        //
        //   System.ComponentModel.Win32Exception:
        //     fileName 不是可执行 (.exe) 文件。
        //
        //   System.ComponentModel.Win32Exception:
        //     打开关联的文件时发生了错误。
        //
        //   System.ObjectDisposedException:
        //     该进程对象已被释放。
        public static Process Start(string fileName, string arguments, string userName, SecureString password, string domain);

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新时代丘鸣山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值