C#程序执行EXE文件

原文地址:http://blog.163.com/qiaoweipeng@126/blog/static/15737970200862331842368/

C#程序执行EXE文件(调用Delphi)
C#程序作为调用这需要生成一个Progress类,该类提供了调用EXE可执行文件所用到的属性和事件.
System.Diagnostics.Process pExecuteEXE  =   new  System.Diagnostics.Process();
pExecuteEXE.StartInfo.FileName 
=   @" E:\Delphi.exe " ;
pExecuteEXE.StartInfo.Arguments 
=   " 'paramstr1 paramstr2,paramstr3' " ;
pExecuteEXE.Start();
pExecuteEXE.WaitForExit();
// 无限期等待完成
// pExecuteEXE.WaitForExit(10000); // 等待最长10秒钟完成。
Delphi可执行程序作为被调用程序,主要是接收参数信息,执行程序,由于程序执行程序完毕不能返回给调用程序信息,只能将信息写入某一位置等待调用者读取。
procedure    TForm1 . Button1Click(Sender :     TObject);   
    
var   
       i
: integer;   
begin   
     self
. Caption    := '' ;   
    
for  i := 0  to paramcount  do
     begin   
         self
. Caption    := self . Caption +   ' [ ' + inttostr(i) + ' : ' + paramstr(i) + ' ] ' ;   
     end;   
    
end;   

完成后退出。

这里是一个简单的调用例子,可以效仿:
*   功         能:通过C#程序调用   Windows   记事本程序   编辑一个
*   名为   test.txt   的文本文件。
*
*   在整个程序中   System.Diagnostics.Process.Start(Info)  
*   为主要语句。
*   如果只是单独执行一个外部程序,可用一条如下代码即可:
*   System.Diagnostics.Process.Start(
*   "外部程序名","启动参数");
*/

using   System;

class   test
{
static   void   Main()
{

//声明一个程序信息类
System.Diagnostics.ProcessStartInfo   Info   =   new   System.Diagnostics.ProcessStartInfo();

//设置外部程序名
Info.FileName   =   "notepad.exe";

//设置外部程序的启动参数(命令行参数)为test.txt
Info.Arguments   =   "test.txt";

//设置外部程序工作目录为   C:\
Info.WorkingDirectory   =   "C:\\";

//声明一个程序类
System.Diagnostics.Process   Proc   ;

try
{
//
//启动外部程序
//
Proc   =   System.Diagnostics.Process.Start(Info);
}
catch(System.ComponentModel.Win32Exception   e)
{
Console.WriteLine("系统找不到指定的程序文件。\r{0}",   e);
return;
}

//打印出外部程序的开始执行时间
Console.WriteLine("外部程序的开始执行时间:{0}",   Proc.StartTime);

//等待3秒钟
Proc.WaitForExit(3000);

//如果这个外部程序没有结束运行则对其强行终止
if(Proc.HasExited   ==   false)
{
Console.WriteLine("由主程序强行终止外部程序的运行!");
Proc.Kill();
}
else
{
Console.WriteLine("由外部程序正常退出!");
}
Console.WriteLine("外部程序的结束运行时间:{0}",   Proc.ExitTime);
Console.WriteLine("外部程序在结束运行时的返回值:{0}",   Proc.ExitCode);
}

另外一篇原文地址:http://www.cnblogs.com/xiaoyusmile/archive/2011/12/08/2280911.html

1. 如果exe文件的返回值是int类型,标识操作执行的结果是否成功,例如:

class Program

    {

        static int Main(string[] args)

        {

            return args.Length;

        }

 }

则在调用exe文件时,可以用如下方法:

Process myProcess = new Process();

string fileName = @"C:/Test.exe";

string para =@"你好 北京欢迎你!";

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(fileName, para);

myProcess.StartInfo = myProcessStartInfo;

myProcess.Start();

while (!myProcess.HasExited)

{

   myProcess.WaitForExit();

}

int returnValue = myProcess.ExitCode;

 

2. 如果exe文件是将输出内容写入标准流,例如:

class Program

    {

        static void Main(string[] args)

        {

            Console.Write(args[0] + args[1] + args [2]);           

        }

 }

则在调用exe文件时,可以用如下方法:

string fileName = @"C:/Test.exe";

Process p = new Process();

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = fileName;

p.StartInfo.CreateNoWindow = true;

p.StartInfo.Arguments = "你好, 北京 欢迎你!";//参数以空格分隔,如果某个参数为空,可以传入””

p.Start();

p.WaitForExit();

string output = p.StandardOutput.ReadToEnd();

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值