WinForm/C# C# 调用 exe 文件

wangbaochen · 2021年09月04日 · 153 次阅读

WinForm 系统方案

通过配置通用模块【APP.WnEmbeddedForm】打开 exe 文件,此方案无法传参,需将文件放入 WinForm 所在文件夹中。

配置参数:ParamInfo
Folder:exe 文件所在的文件夹
File:exe 文件名称
ModuleName:模块打开时所展示的名称

e.g.:

代码方案

调用外部 exe 并且传参,无返回值。


public static void RunExeByProcess(string exePath, string argument)
{
 //创建进程
 System.Diagnostics.Process process = new System.Diagnostics.Process();

 //调用的exe的名称
 process.StartInfo.FileName = exePath;

 //传递进exe的参数 -- 参数以空格分隔,如果某个参数为空,可以传入”” 
 process.StartInfo.Arguments = argument;

 //是否需要系统shell调用程序
 process.StartInfo.UseShellExecute = false;

 //不显示exe的界面
 process.StartInfo.CreateNoWindow = true;

 //输出
 process.StartInfo.RedirectStandardOutput = true;
 process.StartInfo.RedirectStandardInput = true;
 process.Start();

 process.StandardInput.AutoFlush = true;

 //阻塞等待调用结束
 process.WaitForExit();
}

调用外部 exe 并且传参,有返回值。

public static string RunExeByProcess(string exePath, string argument)
  {
   //创建进程
   System.Diagnostics.Process process = new System.Diagnostics.Process();

   //调用的exe的名称
   process.StartInfo.FileName = exePath;

   //传递进exe的参数
   process.StartInfo.Arguments = argument;
   process.StartInfo.UseShellExecute = false;

   //不显示exe的界面
   process.StartInfo.CreateNoWindow = true;
   process.StartInfo.RedirectStandardOutput = true;
   process.StartInfo.RedirectStandardInput = true;
   process.Start();

   process.StandardInput.AutoFlush = true;

   string result = null;
   while (!process.StandardOutput.EndOfStream)
   {
    result += process.StandardOutput.ReadLine() + Environment.NewLine;
   }

   process.WaitForExit();
   return result;
  }
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册